日期:2014-05-20  浏览次数:20711 次

关于异常的一个问题
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();

正常往下执行异常处理,并退出方法,需要返回值。