#871 – Where Execution Continues when an Exception Is Not Caught
June 21, 2013 Leave a comment
When an exception is not caught by a try-catch statement, the code that follows the try-catch statement will not execute.
In the example below, Main() calls MethodA, which calls MethodB. MethodB throws an exception, which is not caught by MethodA. The exception bubbles back up to Main, where it is caught. The code in MethodA that follows the try-catch statement is not executed.
static void Main(string[] args) { try { MethodA(); } catch (Exception xx) { Console.WriteLine("Caught exception in Main()"); } Console.ReadLine(); } private static void MethodA() { try { Console.WriteLine("Before MethodB call"); MethodB(); Console.WriteLine("After MethodB call"); } catch (ArgumentOutOfRangeException argExc) { Console.WriteLine("MethodA catches ArgumentOutOfRangeException"); } Console.WriteLine("In MethodA, after try-catch"); } private static void MethodB() { throw new FileNotFoundException("Can't find your file"); return; }