Home c# c # try catch nested

c # try catch nested

Author

Date

Category

How the finally block is processed if the construction is in progress

try {
  try {}
  catch {}
  finally {}
}
catch {}
finally {}

Will this and that block work, or will it?


Answer 1, authority 100%

Both finally should work, because block finally Always works (see documentation ) if the exception is caught. If not caught, then there are no execution guarantees.


Answer 2, authority 60%

static void Main (string [] args)
{
  try {
    try {
      throw new Exception ();
    }
    catch {
      Console.WriteLine ("Inner catch");
    }
    finally {
      Console.WriteLine ("Inner finally");
    }
  }
  catch {
    Console.WriteLine ("Outer catch");
  }
  finally {
    Console.WriteLine ("Outer finally");
  }
}

Result:

Inner catch

Inner finally

Outer finally

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions