【问题标题】:Where to put finally in nested try/catch?最后在哪里放置嵌套的try/catch?
【发布时间】:2012-11-29 22:05:28
【问题描述】:

finally 如何在嵌套的try/catch 中工作?
例如。为:

try{  
//code

}  
catch(SomeException e){  
   //code  
   try{  
      //code  
   }  
   catch(OtherException e){  
     //code  
   }  
}  
catch(SomeOtherException e){    
  //code  
} 

放置finally 的最佳位置在哪里?或者我也应该把它放在嵌套和外部try 中?

【问题讨论】:

  • 不清楚结果应该是什么。 finally 块可以放置在每个 try-catch 上。
  • 所以如果我将finally 放在外部try 中,它会被执行吗?我正在为此寻找更清晰的方法
  • 看答案有一些很好的解释。

标签: java exception coding-style try-catch


【解决方案1】:

如果您希望 finally 块中的代码无论在任一块中发生什么都运行,请将其放在外部 try 上。如果您只希望它在第一个 try发生什么情况下运行,请将其放在那里:

try{  // try/catch #1
  //code block #1

}  
catch(SomeException e){  

   //code block #2

   try{  // try/catch #2
      //code block #3
   }  
   catch(OtherException e){  
     //code block #4
   }  
   finally {
     // This code runs no matter what happens with try/catch #2 (so
     // after code block #3 and/or #4, depending on whether there's
     // an exception). It does NOT run after code block #1 if that
     // block doesn't throw, does NOT run after code block #2 if
     // that block DOES throw), and does not run if code block #1
     // throws SomeOtherException (code block #5).
   }
}  
catch(SomeOtherException e){    
  //code block #5
} 
finally {
  // This code runs no matter what happens with EITHER
  // try/catch #1 or try/catch #2, no matter what happens
  // with any of the code blocks above, 1-5
}

更简单地说:

try {
   // covered by "outer" finally
}
catch (Exception1 ex) {
   // covered by "outer" finally

   try {
     // Covered by both "inner" and "outer" finallys
   }
   catch (Exception2 ex) {
     // Covered by both "inner" and "outer" finallys
   }
   catch (Exception3 ex) {
     // Covered by both "inner" and "outer" finallys
   }
   finally { // "inner" finally
   }
}
catch (Exception4 ex) {
   // covered by "outer" finally
}
finally { // "outer" finally
}

【讨论】:

    【解决方案2】:

    这取决于您希望 finally 块中的代码执行的位置。

    try{  //Try ROOT
    //code
    
    }  
    catch(SomeException e){  //Catch ROOT ONE
       //code  
       try{  //Try NEST
          //code  
       }  
       catch(OtherException e){  //Catch NEST
         //code  
       }
       finally{
         //This will execute after Try NEST and Catch NEST
       }
    }  
    catch(SomeOtherException e){    //Catch ROOT TWO
      //code  
    }
    finally{
      //This will execute after try ROOT and Catch ROOT ONE and Catch ROOT TWO
    }
    

    没有最好的地方取决于您想要的功能。但是,如果您希望 finally 块仅在所有 try catch 块之后运行,那么您应该将它放在最终根 catch 块之后。

    【讨论】:

      【解决方案3】:

      来自Doc

      finally 块总是在 try 块退出时执行。这可以确保即使发生意外异常也会执行 finally 块。但 finally 不仅仅对异常处理有用——它允许程序员避免清理代码意外地被 return、continue 或 break 绕过。将清理代码放在 finally 块中始终是一种很好的做法,即使没有预料到异常也是如此。

      最好把 finally 放在需要的地方。

      try{  
        //code
      
       }    
       catch(SomeException e){  // 1
           try{  //Try Block 
             //code  
            }  
           catch(OtherException e){  //2
             //code  
            }
           finally{
             //This will execute after Try Block and Catch Block 
            }
       }  
      catch(SomeOtherException e){  //3
        //code  
       }
      finally{
         //This will execute after 1 and 2 and 3
       }
      

      欲了解更多详情,请查看以下链接。

      1. The finally Block
      2. Does a finally block always run?

      【讨论】:

      • "最好把 finally 放在所有 catch 块之外。"不,最好把它放在有意义的地方。如果 finally 只需要为内部块运行,则将其与内部块放在一起。您的陈述还假设最终只有 1 个,但不一定如此。
      猜你喜欢
      • 2022-01-13
      • 1970-01-01
      • 2010-10-06
      • 2016-07-02
      • 1970-01-01
      • 2015-02-08
      • 2021-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多