【发布时间】:2018-04-10 01:31:09
【问题描述】:
在 try 块中,我希望状态变量将更改为 true 并发送成功返回。我不想发送错误的返回类型。
相反,如果返回类型为 false,我想抛出错误。 我可以遇到发送错误返回类型的情况。
public boolean updateSchool(String username, String password, String school) throws Exception {
User user = userDao.findByUsername(userName);
boolean status = false;
try {
if (user != null && user.getPassword().equals(password)) {
user.setSchool(school);
userDao.save(user);
status = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return status;
}
【问题讨论】:
-
简单。不要捕获异常。它将向上传递到堆栈。
-
@TrippKinetics 它被重新抛出。
-
您显然知道如何使用
if以及如何抛出异常。你的问题到底是什么? -
顺便说一句,如果返回值始终相同,那么它的意义何在?
-
在 try 块中,返回类型设置为 true。我不希望状态具有错误值,这就是我想抛出错误的原因。