读书人

关于错误的一个有关问题

发布时间: 2012-03-24 14:00:46 作者: rapoo

关于异常的一个问题
public User find(String username,String password){
try {
……………………
return user;


} catch (DocumentException e) {
// throw new RuntimeException(e);
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
会提示必须返回User类型的结果
而修改为
public User find(String username,String password){
try {
……………………
return user;


} catch (Exception e) {
throw new RuntimeException(e);
}


}
却没有报错,请问为什么?return语句不都是在try中吗?

[解决办法]
在catch里不要throw了
[解决办法]
throw new RuntimeException(e);抛出一个异常,异常退出当前执行方法(当然无所谓返回值了)。
…………
catch (DocumentException e) {
// throw new RuntimeException(e);
e.printStackTrace();
}
正常往下执行异常处理,并退出方法,需要返回值。

读书人网 >J2EE开发

热点推荐