#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

#707 – More Than One Class in A Program Can Contain A Main Method

You typically have one class in your program that contains a function named Main, which acts as the entry point for the application.  The entry point is the first method that will execute when you start your application.

    class Program
    {
        static void Main(string[] args)
        {
            Dog d = new Dog("Kirby", 15);
            d.Bark();
        }
    }

You can actually have more than one class that contains a Main method.  If you do this, however, you must tell the compiler which class contains the Main that it should use as an entry point.  You specify this in the project’s Properties window.

    class ADifferentProgram
    {
        static void Main(string[] args)
        {
            Dog d2 = new Dog("Ruby", 2);
            d2.Bark();
        }
    }


#10 – The Return Value from Main() Sets ERRORLEVEL Variable in Windows

If your Main() function returns an integer value, you can check that value in the calling program or script using the ERRORLEVEL environment variable.  By convention, a return value of 0 indicates success and any other value indicates an error.  Below is a sample .bat file that calls a program called MyProgram and then checks the return value.

@echo off

MyProgram

IF "%ERRORLEVEL%" == "0" goto OK

:NotGood
    echo Bad news.  Program returned %ERRORLEVEL%
    goto End

:OK
    echo Everything A-OK

:End

#9 – Main() Should Not Be Public

The following code will compile, but is not recommended, according to the MSDN documentation. If Main() is public, other classes could call it.  But this violates the intent for Main(), which is meant to only be called once, when the program is invoked.

class Program
{
    static public void Main()
    {
        Console.WriteLine("This would compile..");
    }
}

#8 – The Main() Function Can Return an int

Instead of returning void, Main() can return an integer value, which can then be read by the calling program or script.  Notice that the return type of the function is an int

class Program
{
    static int Main()
    {
        Console.WriteLine("Doing something..");

        if (DateTime.Today.DayOfWeek == DayOfWeek.Monday)
            return -1;     // Monday is bad
        else
            return 0;
    }
}

#6 – An Even Smaller C# Program

In The Smallest Possible C# Program, I mentioned a couple things as optional.  For the record, here’s the absolute smallest C#.NET program that you can write.  (Assuming that you don’t need it to actually do anything).

class Program
{
    static void Main()
    {
    }
}

#2 – The Smallest Possible C# Program

The smallest possible C# program would consist of a static Main() function inside of a class. The namespace is actually optional, as is the args parameter.

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("2,000 Things Was Here..");
        }
    }
}