What is a "first chance exception"?
Asked Answered
W

5

125

What exactly is a first chance exception? How and where does it originate in a .NET program? And why is it called by that peculiar name (what 'chance' are we talking about)?

Willetta answered 19/2, 2009 at 10:33 Comment(0)
R
95

It's a debugging concept. Basically exceptions are thrown to the debugger first and then to the actual program where if it isn't handled it gets thrown to the debugger a second time, giving you a chance to do something with it in your IDE before and after the application itself. This appears to be a Microsoft Visual Studio invention.

Rego answered 19/2, 2009 at 10:37 Comment(5)
It's more than a debugging concept; C# doesn't provide a convenient means of observing two-pass exception handling at run-time, but vb.net does. Basically, when an exception is thrown, the run-time starts by searching up the call stack to find out who if anyone is going to catch it. That process takes place before any finally blocks run. Once the system decided who's going to catch an exception (and determined that someone is actually going to), the system will start unwinding the stack. Note that if an exception is thrown from a finally block...Foredeck
...the code which was expecting to catch the original exception may end up not doing so; there are many weird corner cases.Foredeck
This happens when you have configured the debugger to break at all thrown exceptions (not just unhandled exceptions), or if you "step" into some statement that throws. You see the message A first chance exception of type 'foo' occurred in YourApp.exe. You can still continue (F5) or step further (F11). Then if there is a catch for this, control goes there. If there is no catch block, you get the "second-chance" break, this time the message is An unhandled exception of type 'foo' occurred in YourApp.exe. From here, trying to continue or step further will not be successful.Hohenstaufen
@supercat: How do I learn about the details like you mentioned in your comments above? Is it a result of self-experimentation? reading books (which one)? That's a very impressive insight! I would appreciate if you could take a moment to answer this please...Cloche
@NoSaidTheCompiler: I've read about exception handling in various blogs; I would expect that a search for "exception filter .net" should turn up some blogs on the subject.Foredeck
P
25

First chance exception notifications are raised when an exception is thrown. Second chance notifications are when it is not caught. (Chance – as in opportunity to break into the code in the debugger).

First and second chance exception handling

Pearlypearman answered 19/2, 2009 at 10:40 Comment(0)
H
7

I just started using the debugger and ran into this. In my research, I found the MSDN blog post What is a First Chance Exception? that cleared it up for me.

The big takeaways from the blog post for me are that it refers to notification to the debugger, and not something my code would necessarily need to handle, and most importantly,

"First chance exception messages most often do not mean there is a problem in the code."

Hallah answered 8/7, 2014 at 5:36 Comment(0)
U
1

From a developer's perspective, it's more concerning a second-chance exception, because it would mean it was not handled in code; therefore the application would stop.

First chance could be many of them, but the ones to concern about more, again, from a development perspective, are second chance, because it would lead to an application crash.

Underglaze answered 13/4, 2015 at 13:15 Comment(0)
S
0

When an application is being debugged, the debugger gets notified whenever an exception is encountered. At this point, the application is suspended and the debugger decides how to handle the exception. The first pass through this mechanism is called a "first chance" exception.

Depending on the debugger's configuration, it will either resume the application and pass the exception on or it will leave the application suspended and enter debug mode. If the application handles the exception, it continues to run normally.

First chance exception messages most often do not mean there is a problem in the code. For applications / components which handle exceptions gracefully, first chance exception messages let the developer know that an exceptional situation was encountered and was handled.

Scrivenor answered 31/7, 2014 at 13:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.