如下就不会执行catch语句。
@RequestMapping("/single_importNc"
)
@ResponseBody
public
Object importNc(
@RequestParam
String
singleId)
{
Map
map
=
new
HashMap();
try{
throw
new
RuntimeException("shibai!");
}
catch(
SQLException
e){
map.put("msg",
e
.getMessage());
}
return map;
}
以下都是在controller层写的:
如下写法会执行catch语句,会把e
.getMessage()得到的String形式的错误信息返回给前台。
try{
throw
new
RuntimeException("shibai!"
);
}
catch(
Exception
e){
map.put("msg",
e
.getMessage());
}
如果这样写:
try{
throw
new
Exception("shibai!"
);
}
catch(
RuntimeException
e){
map.put("msg",
e
.getMessage());
}
throw
new
Exception("shibai!"
); 这一行会报错,因为抛出的是Exception,catch住的异常类型是RuntimeException,不对。
在方法头加throws Exception,就对了。或者
改成把catch异常类型换成Exception,如下:
try{
throw
new
Exception("shibai!"
);
}
catch( Exception
e){
map.put("msg",
e
.getMessage());
}