首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

关于错误的一个有关问题

2012-03-24 
关于异常的一个问题public User find(String username,String password){try {……………………return user} catc

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

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

热点排行