Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9005885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:15:49+00:00 2026-06-16T01:15:49+00:00

How does finally work in nested try/catch ? E.g. for: try{ //code } catch(SomeException

  • 0

How does finally work in nested try/catch?
E.g. for:

try{  
//code

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

Where is the best place to put finally? Or should I put it in nested and outer try as well?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:15:50+00:00Added an answer on June 16, 2026 at 1:15 am

    If you want the code in the finally block to run no matter what happens in either block, put it on the outer try. If you only want it to run no matter what happens within the first try block, put it there:

    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
    }
    

    More briefly:

    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
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C# , how does a try catch finally block work? So if there
How does a return statement inside a try/catch block work? function example() { try
How should I structure the try/finally when using a SemaphorSlim with cancellation token so
I finally got some parts working here: http://jsfiddle.net/trXBr/5/ but when I put the code
I'm reviewing some new code. The program has a try and a finally block
So I finally got my urls to work using nested resources but there's still
Why does this code not work? I am writing an application that has ability
I've finally found a way to get this to work but the code it
The following code does not work all the time. I have looked at countless
What exactly does a finally block in exception handling perform?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.