Java 在嵌套的try/catch中最后放在哪里?

Java 在嵌套的try/catch中最后放在哪里?,java,exception,coding-style,try-catch,Java,Exception,Coding Style,Try Catch,最终如何在嵌套的try/catch中工作? 例如: try{ //code } catch(SomeException e){ //code try{ //code } catch(OtherException e){ //code } } catch(SomeOtherException e){ //code } 最后放置的最佳位置是哪里?或者我应该把它放在嵌套的和外部的try

最终如何在嵌套的
try/catch
中工作?
例如:

try{  
//code

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

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

如果你想让
最后的
块中的代码运行,不管在任何一个块中发生什么,把它放在外部的
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
}

当try块退出时,finally块始终执行。这确保即使发生意外异常,也会执行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
 }
有关更多详细信息,请查看以下链接


  • 这取决于您希望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块之后。

    不清楚结果应该是什么。
    finally
    块可以放在每个
    try catch
    上。因此,如果我把
    finally
    放在外部
    try
    上,它会被执行吗?我正在寻找更清晰的方法看看答案,有一些很好的解释。“最好把你的finally放在所有catch块的外面。”不,最好把它放在有意义的地方。如果finally只需要为内部块运行,则将其与内部块放在一起。您的声明还假设只有1个finally,但事实并非如此。