Consider this simple console application:
using System;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
throw new Exception();
}
}
}
I run this under the debugger in either Visual Studio 2010 or Visual Studio 2012 Beta.
When I do so, naturally the debugger stops at the exception. Ok so far.
But when I press F5 to continue (or choose Debug|Continue) it stops at the same exception again. I have to stop debugging for the program to exit. I expected the program to exit when I pressed F5.
Does anyone know why it behaves the way that it does?
[EDIT]
I've marked a reply as an answer, but to see a weird consequence of the debugger's behaviour, consider the following code:
using System;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
throw new Exception();
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Console.WriteLine("Unhandled Exception.");
}
}
}
Run this under the debugger and press F5 a load of times, then look at the output. You'll see a lot of "Unhandled Exception" messages, despite the code only actually throwing it once. The debugger is causing the exception to be thrown multiple times! This is what I find strange.
What did you expect?
Consider the following method:
private void Test()
{
throw new Exception();
int u = 4;
}
When the exception is thrown, the debugger allows you to navigate to the calling context to see if your program catches the exception. If it's not the case, it never exits the Test
method by skipping the exception, that's why int u = 4;
is unreachable.
In your example, it's the same:
private static void Main(string[] args)
{
throw new Exception();
// If I'm here, I will exit the application !
// But this place is unreachable
}
You can't exit the Main
method scope because of your exception. That's why you can't exit your application while debugging using F5.
If you have no debugger attached, your application will of course crash because of an unhandled exception, but this is another story.
It doesn't do that. If the application wasn't running in the debugger, you'd get an "application quit unexpectedly" dialog - but not in Visual Studio.
While I'm not exactly sure why it behaves like that, it may be because it gives you the option to move the "currently executing line" arrow (the yellow array) to the next line that should be executed and to resume operation.
Otherwise, yes, you need to stop the application explicitly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With