Maybe when you debug the program in Viusal studio, you find that output windows prints out a line of information about a first chance exception of type ‘ System.NullReferenceException ‘occurred, you may have wondered what is the first chance exception, Does this sentence mean that there is something wrong with my code?But if there is a problem, why does my program not throw any exception but display such a message in output windows?If I don’t look at the output window, I may not find this message at all.
Let’s talk about the first chance exception. When you are debugging a program in VS, when there is a problem with the program, it is the debugger (which can also be understood as vs itself) who first catches the exception. At this time, vs has to decide how to solve the problem.
- is to stop now, throw out the problem, pop up a dialog box, tell the user that there is a problem in your program, and let the user decide whether to continue or stop.
- leave it alone. If the user has a try catch, let the user’s own code process it. If there is no try catch, let the runtime capture it and then let the runtime process it. But when debugging, it is still vs to capture again.
So what does vs rely on to decide how to deal with it?Under debug settings, if CLR exception is checked, vs will catch an exception. It will not only print a message in output windows, but also pop up a dialog box to notify the user.
If there is no check here, then vs does nothing, just like there is no such thing.
For example, like the following code,
If vs catches and throws, the program stops at line 36 and notifies the user. If vs lets it go, the program jumps to line 39 and continues to execute the processing logic of the code.
Two concepts need to be understood. When C # programs are really running, it is runtime. As the top manager, exception will be thrown here. When debugging in VS, VS Runtime is the top manager, exception will throw it here, so that VS can notify users through the form of dialog box. First chance exception will inform the user through the pop-up box. If the user selects continue, the program can continue to run. However, if the code does not have a corresponding capture mechanism to deal with it, it is thrown to vs runtime again and captured, then it is called second chance exception. At this time, a dialog box will pop up again, but this time, the code will not continue to run.
Moreover, it should be noted that if we do not check the above option, vs will throw an exception dialog box on line 39 when calling the program.
But if we check it, we will throw an exception dialog box where the 52 line exception actually occurs.
Some people will ask if I should check it?If your project references a heavyweight tool like NHibernate, the DLL itself is likely to throw exceptions in many places, but the DLL itself has try If you check “catch”, vs will capture the catch code block before it is processed, and then inform the user. There will be a lot of such cases. As a result, the user will point to a lot of continue dialog boxes. Don’t check it at this time.
Reference link:
https://blogs.msdn.microsoft.com/davidklinems/2005/07/12/what-is-a-first-chance-exception/
http://dzimchuk.net/post/when-a-first-chance-exception-of-type-xxx-occurred-you-really-have-a-bug
Read More:
- Programmers encounter real wonderful interview experience! Did you win? |Programmers have something to say
- Fatal error in GC getthreadcontext failed bug exception.
- Bug resolution of 0xc0000005: access conflict occurred when reading location of 0x00000000.
- net start mysql System error 2 has occurred. The system cannot find the file specified.
- The solution of a Java exception has occurred. And error exception in thread when eclipse runs
- [Vue warn]: Unknown custom element: <el-carousel-item> – did you register the component correctly?
- error: multiple substitutions specified in non-positional format; did you mean to add the formatted
- Vue error: did you register the component correctly? For.., make sure to provide the “name” option (solved)
- Type error: cannot unpack non Iterable non type object appears when starting Bert server in Ubuntu system
- Unknown custom element: <component> – did you register the component correctly
- [Handling Notes] Alternative reasons for Configuration system failed to initialize exception
- Fatal error C1010: unexpected end of file encountered while looking for precompiled header. Did you forget to add “ා include” StdAfx. H to the source
- “An error occurred while adding a recovery system to the destination disk
- Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable.
- Running Python 3.7 web.py Runtimeerror: generator raised stopiteration exception occurred during test
- python reads csv file is an error _csv.Error: iterator should return strings, not bytes (did you open the file in text)
- ImageIO.read () unsupported image type when reading picture_ exception Unsupported Image Type
- Destructor abnormal stuck bug
- A fatal error occurred while trying to sysprep the computer system
- An error occurred when MySQL backup with mysqldump instruction in MacOS system