views:

268

answers:

2

This is a behavior I noticed several times before and I would like to know the reason behind that. If you run the following program under the (Visual Studio 2008) debugger, the debugger will keep breaking on the throw statement no matter how often you continue debugging. You have to stop debugging to get out there. I would expect that the debugger breaks once and then the process terminates as it happens if you run the program without the debugger. Is anybody aware of a good reason for this behavior?

using System;

namespace ExceptionTest
{
   static internal class Program
   {
      static internal void Main()
      {
         try
         {
            throw new Exception();
         }
         catch (Exception exception)
         {
            Console.WriteLine(exception.Message);

            // The debuger  will never go beyond
            // the throw statement and terminate
            // the process. Why?
            throw;
         }
      }
   }
}
A: 

You cannot step past an exception that bubble all the way to the 'top' with out being handled Try this if you really need it:

static internal void Main()
        {
            try
            {
                throw new Exception();
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);

                // The debuger  will never go beyond
                // the throw statement and terminate
                // the process. Why?
                Environment.FailFast("Oops");
                throw;
            }
        }
Jan Bannister
The question is why I cannot do this and terminate the process.
Daniel Brückner
+1  A: 

Stepping over the unhandled exception would terminate the process - its probably just to stop you from accidentally terminating the process when you didnt intend to.

If the exception is handled elsewhere (such as in a generic outer try-catch block) then you will be able to step over the exception and the debugger will take you to the place where it is handled.

Kragen