What is a “first chance exception”?

2019-01-03 13:05发布

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)?

5条回答
做个烂人
2楼-- · 2019-01-03 13:33

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.

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-03 13:33

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

查看更多
forever°为你锁心
4楼-- · 2019-01-03 13:43

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.

查看更多
祖国的老花朵
5楼-- · 2019-01-03 13:52

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.

查看更多
仙女界的扛把子
6楼-- · 2019-01-03 13:55

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."

查看更多
登录 后发表回答