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 4323818
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:03:04+00:00 2026-05-21T09:03:04+00:00

Here’s a pseudo-code: try { TextReader tr = new StreamReader(filename); try { string content

  • 0

Here’s a pseudo-code:

try
{
  TextReader tr = new StreamReader("filename");
  try
  {
    string content = tr.ReadToEnd();
  }
  catch(Exception ex)
  { /*Show error message*/ }
  finally
  { tr.Close();}
}
catch(Exception ex)
{ /*Show error message*/ }

Ok, that code is a sample of a book. What i don’t understand is why a nested try-catch block is used.

If

string content = tr.ReadToEnd();

goes wrong then outer catch should catch the exception right? So I can’t see any point of using extra try-catch for that line!

If you see any misconceptions here then I’d be glad if you could point them out!

Also are there any cases where nested try-catch would be reasonable to use?

  • 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-05-21T09:03:05+00:00Added an answer on May 21, 2026 at 9:03 am

    Differences between your example and similar code in which there is only one, outer, catch:

    1) If the inner block throws, then the message is printed before calling Close(). This might be significant if Close() also contains logging.

    2) If an inner exception is thrown and caught, and then in the finally block Close() also throws, you get two exceptions and you handle two exceptions. With only one catch block, if Close() throws then what happens next might depend on language, but you won’t get two error messages printed. I’d expect the second exception to replace the first one, but I don’t swear that’s what happens in all languages with finally, or even all languages I’ve used…

    3) //Show error message is just a comment, it does nothing. It may be the intention of the author that you’d show a different error message in the two different cases. The inner one would say, “read failed”, and the outer one would say, “cannot open file”. Or something like that. To achieve that with only one catch block, you could set and check a flag indicating progress (which probably doesn’t make the code any simpler than two catch blocks), or you could rely on the exception itself to contain the appropriate error message (in which case good luck with integrating your localization framework with exception-throwing functions in built-in or third-party libraries).

    Note that even if you only have one catch, you still need two tries because of the finally. This is no good:

    try {
        TextReader tr = new StreamReader("filename");
        string content = tr.ReadToEnd();
    } catch(Exception ex) {
        // show error message
    } finally {
        tr.Close();
    }
    

    Because by the looks of this language, tr will not be in scope in the finally block, so we can’t close it there. We have to close it inside the block that deals with creating it. It may be that we could deal with it like this:

    TextReader tr = null;
    try {
        tr = new StreamReader("filename");
        string content = tr.ReadToEnd();
    } catch(Exception ex) {
        // show error message
    } finally {
        if (tr != null) {
            tr.Close();
        }
    }
    

    But that doesn’t really simplify things much compared with the original code, we still have differences 2-3 to contend with, and now we don’t handle exceptions from Close() at all. So for many purposes the original code with multiple try/catch was better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here are 2 screen shots when i try to debug my code in visual
Here a simple question : What do you think of code which use try
Here is some simple code: DIR* pd = opendir(xxxx); struct dirent *cur; while (cur
Here's the code in AlertTableView: - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ NSInteger index = 12345; NSLog(@AlertTableView:
Here is an example: I write html code inside of textarea, then I swap
Here is the css: #content ul { font-size: 12px; } I am trying this:
Here is my code, which takes two version identifiers in the form 1, 5,
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here is my code sample, let me know if it can be further improved?
Here is my code (Say we have a single button on the page that

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.