据我所知,以下两个代码片段都将起到同样的作用.为什么要有finally块?
代码A:
try { /* Some code */ }
catch { /* Exception handling code */ }
finally { /* Cleanup code */ }
Run Code Online (Sandbox Code Playgroud)
代码B:
try { /* Some code */ }
catch { /* Exception handling code */ }
// Cleanup code
Run Code Online (Sandbox Code Playgroud) 我从来没有正确理解finally语句的用法.谁能告诉我两者之间的区别是什么:
try {
a;
block;
off;
statements;
} catch (Exception e) {
handle;
exception;
e;
} finally {
do;
some;
cleanup;
}
Run Code Online (Sandbox Code Playgroud)
一方面和:
try {
a;
block;
off;
statements;
} catch (Exception e) {
handle;
exception;
e;
}
do;
some;
cleanup;
Run Code Online (Sandbox Code Playgroud)
在另一
如果将要执行catch块之后的语句,那么java中finally块的真正用途是什么?例
try {
//Code
}
catch (Exception e)
{
//Code
}
finally {
System.out.println("anyway it will be executed");
}
System.out.println("anyway it will be executed");
Run Code Online (Sandbox Code Playgroud)