最近没事在看java核心卷 第八版 看到异常处理的时候 问题来了 如下: Craphics g = image.getGraphice();try{
//1 //2
}这个说真的没看懂,try语句块里的东西 都是一次执行 怎么又来个try2 描述是这样的: 首先执行try语句块中的全部代码,然后执行finally子句中的代码.随后,继续执行try语句块之后的第一条语句(也就是try1)请前辈们 解释下 谢谢.
//1 //2
}这个说真的没看懂,try语句块里的东西 都是一次执行 怎么又来个try2 描述是这样的: 首先执行try语句块中的全部代码,然后执行finally子句中的代码.随后,继续执行try语句块之后的第一条语句(也就是try1)请前辈们 解释下 谢谢.
...
}finally{
...
}
String str = new String("首先执行try语句块中的全部代码,然后执行finally子句中的代码.随后,继续执行try语句块之后的第一条语句(也就是try1),也就是这里");
//1 //2
}catch(IOException e){
//3
show error dialog
//4
}
finally{
//5
g.dispose();
}
//6
那try1不是被执行过了么。
Graphics g = image.getGraphics();
TRy
{
// 1
code that might throw exceptions
// 2
}
catch (IOException e)
{
// 3
show error dialog
// 4
}
finally
{
// 5
g.dispose();
}
// 6
Let us look at the three possible situations in which the program will execute the finally clause.The code throws no exceptions. In this event, the program first executes all the code in the TRy block. Then, it executes the code in the finally clause. Afterwards, execution continues with the first statement after the try block. In other words, execution passes through points 1, 2, 5, and 6.The code throws an exception that is caught in a catch clause, in our case, an IOException. For this, the program executes all code in the try block, up to the point at which the exception was thrown. The remaining code in the try block is skipped. The program then executes the code in the matching catch clause, then the code in the finally clause.If the catch clause does not throw an exception, then the program executes the first line after the try block. In this scenario, execution passes through points 1, 3, 4, 5, and 6.If the catch clause throws an exception, then the exception is thrown back to the caller of this method, and execution passes through points 1, 3, and 5 only.The code throws an exception that is not caught in any catch clause. For this, the program executes all code in the try block until the exception is thrown. The remaining code in the try block is skipped. Then, the code in the finally clause is executed, and the exception is thrown back to the caller of this method. Execution passes through points 1 and 5 only.看看吧 很好理解 可能是 翻译的让楼主 有了错误的理解 看看原版!