try
块也可以有零个或一个finally
块。 finally
块总是与try
块一起使用。
语法
finally
块的语法是:
1
2
3
|
finally { // Code for finally block } |
finally
块以关键字finally
开始,后面紧跟一个大括号和一个大括号。finally
块的代码放在大括号内。
try
,catch
和finally
块有两种可能的组合:try-catch-finally
或try-finally
。try
块可以后跟零个或多个catch
块。try
块最多可以有一个finally
块。try
块必须有一个catch
块和一个finally
块或两者都有。try-catch-finally
块的语法是:
1
2
3
4
5
6
7
8
9
|
try { // Code for try block } catch (Exception1 e1) { // Code for catch block } finally { // Code for finally block } |
try-finally
块的语法是: