原始代码如下
public String func() throws IOException {
String res = "_Empty";
try{
//IOFunction()
// the function will throw IOException
res = "Success";
} catch ( ParseException e) {
e.printStackTrace();
} finally {
return res;
}
}
我在调试这段代码的时候,函数返回结果总是_Empty,但是又没有打印错误信息或者throw IOException 。无意间发现 func() throws 后面的 IOException 是灰色的,同时IDEA提示,该函数永远不会抛出 IOException 。把 finally 语句去掉才正常:
public String func() throws IOException {
String res = "_Empty";
try{
//IOFunction()
// the function will throw IOException
res = "Success";
} catch ( ParseException e) {
e.printStackTrace();
return res;
}
个人理解,finally 算是对所有 Exception 类型都已经做出了处理,所以不再向上 throws。
欢迎各路大佬批评指正!