AppDomain.FirstChanceException and stack overflow exception
<p>I'm using the <a href="http://msdn.microsoft.com/en-us/library/system.appdomain.firstchanceexception.aspx">FirstChanceException</a> event to log details about any thrown exceptions.</p>

<pre><code>static void Main(string[] args)
{
    AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) =&gt;
    {
        Console.WriteLine("Inside first chance exception.");
    };

    throw new Exception("Exception thrown in main.");
}
</code></pre>

<p>This works as expected. But if an exception is thrown inside the event handler, a stack overflow will occur since the event will be raised recursively.</p>

<pre><code>static void Main(string[] args)
{
    AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) =&gt;
    {
        throw new Exception("Stackoverflow");
    };

    throw new Exception("Exception thrown in main.");
}
</code></pre>

<p>How do I handle exceptions that occur within the event handler?</p>

<p>Edit:  </p>

<p>There's a few answers suggesting that I wrap the code inside the event handler in a try/catch block, but this doesn't work since the event is raised before the exception can be handled.</p>

<pre><code>static void Main(string[] args)
{
    AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) =&gt;
    {
        try
        {
            throw new Exception("Stackoverflow");
        }
        catch
        {
        }
    };

    throw new Exception("Exception thrown in main.");
}
</code></pre>
