In C# can you return a default int value from main?
<p>I want to return a default int value from main. </p>

<p>Consider the following:</p>

<pre><code>using System;
class Class1
{
    static int Main(string[] args)
    {
        int intReturnCode = 1;
        int intRandNumber;

        Random myRandom = new Random();
        intRandNumber = myRandom.Next(0,2);
        if(intRandNumber ==1)
        {
            throw new Exception("ErrorError");      
        }
        return intReturnCode;
    }
}
</code></pre>

<p>When the exception is reached I don't get to set the returncode. </p>

<p>Is it possible to have a default return code inside of main? </p>

<p><strong>Clarification:</strong> I have a program that is throwing Unhandled Exceptions. I have the application inside a try catch, however some errors (probably out of memory, stackoverflow etc) are still bubling up and causing my application to fail in production. </p>

<p>To fix this I've added code to capture unhandled exceptions.</p>

<p>This was added to main:</p>

<pre><code>AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);
</code></pre>

<p>And now I have this method that is reached when an unhandled exception occurs. </p>

<pre><code>public static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)   
{ 
    //here's how you get the exception  

    Exception exception = (Exception)e.ExceptionObject;                 

    //bail out in a tidy way and perform your logging
}
</code></pre>

<p>The prblem is that I'm no longer in Main and I want to exit with a non-zero exit code. </p>
