arv*_*unk 1 java exception-handling exception try-catch try-catch-finally
我正在开展一个项目,我需要执行两个不同的操作.我的主控制器方法中有一个finally块.
我的问题是,我最终可以有两个以上,例如:
class test
{
X()
{
try
{
//some operations
}
finally
{
// some essential operation
}
}
//another method
Y()
{
try
{
//some operations
}
finally
{
// some another essential operation
}
}
}
Run Code Online (Sandbox Code Playgroud)
那么,有可能吗?
每个try/catch/finally语句只能有一个finally子句,但是你可以在同一个方法或多个方法中有多个这样的语句.
基本上,try/catch/finally语句是:
trycatch (0或更多)finally (0或1)...但必须至少有一个catch/ finally(你不能只有一个"裸"的try陈述)
另外,你可以嵌套它们;
// Acquire resource 1
try {
// Stuff using resource 1
// Acquire resource 2
try {
// Stuff using resources 1 and 2
} finally {
// Release resource 2
}
} finally {
// Release resource 1
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15886 次 |
| 最近记录: |