例如:
public String showMsg(String msg) throws Exception {
if(msg == null) {
throw new Exception("Message is null");
}
//Create message anyways and return it
return "DEFAULT MESSAGE";
}
String msg = null;
try {
msg = showMsg(null);
} catch (Exception e) {
//I just want to ignore this right now.
}
System.out.println(msg); //Will this equal DEFAULT MESSAGE or null?
在某些情况下,我需要基本上忽略异常(通常当一个方法可以抛出多个异常,而在特定情况下一个无关紧要),所以尽管我为了简单起见使用了可怜的例子,但 showMsg 中的返回仍然会运行还是 throw 实际上会返回该方法?