#907 – Exceptions Thrown from Main Are Treated as Unhandled Exceptions

When an exception is thrown from a Main method and you don’t catch the exception within the Main method, the exception will be treated as an unhandled exception.  Because the Main method is the entry point, or topmost method, of your application, there is no higher level method where the exception can be handled.

When the unhandled exception occurs, the application will stop executing and display an error message.

In the example below, we throw an exception of type ApplicationException from the Main method, but do not catch the exception in Main.  The exception is dumped to the console output and we see an error dialog.

        static void Main(string[] args)
        {
            if (DateTime.Now.DayOfWeek == DayOfWeek.Monday)
                throw new ApplicationException("Sorry, I don't work on Mondays");

            Console.WriteLine("I'm running normally..");
        }

907-001

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

2 Responses to #907 – Exceptions Thrown from Main Are Treated as Unhandled Exceptions

  1. You should check out http://exceptionless.com. It’s a great service that tracks your unhandled exceptions and allows you to fix them quickly.

  2. Pingback: Dew Drop – August 13, 2013 (#1,602) | Alvin Ashcraft's Morning Dew

Leave a comment