#871 – Where Execution Continues when an Exception Is Not Caught

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;
        }

871-001

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: