A First Chance Exception

偶尔善良 提交于 2019-12-02 01:29:06

问题


've been running through the MSDN help documents to get a hang of Visual Basic. After trying out the example using timers --one drags a label and timer component into the designer and adds the following to the components subroutine

Label1.Text = My.Computer.Clock.LocalTime.ToLongTimeString

The output for the immediate window during debug is the following

A first chance exception of type 'System.InvalidCastException' occured in Microsoft.VisualBasic.dll
A first chance exception of type 'System.InvalidCastException' occured in Microsoft.VisualBasic.dll

The same error occurs on a previous MSDN example using a context menu component. Should I Try...Catch...Finally this error and try to move on? Or, am I dealing with something much more serious?


回答1:


When you see something about a first chance exception, it only means that an exception was caught within the code you called but does not necessarily mean that the code failed. If the code runs without causing your program to crash and returns a valid value, then do not have a problem. You will also see output in the debug window about first chance exceptions when you implement your own try/catch blocks.




回答2:


In the Debug menu -> Exceptions, you can enable the debugger to stop when an Exception is first thrown, even if it would be caught later; if you want to find out what's happening, this is the easiest way to do it




回答3:


In the first chance exception examine the details of the exception. You should see a stack frame/trace property. In there you should see what line the error occurs on. This should help you.




回答4:


In the IDE try going to Tools > Options > Projects and Solutions > VB Defaults and setting Option Strict to 'On' - this may help catch casting problems when you compile your project rather than when you run it.

A 'first chance execption' does not necessarily mean you have a problem in your code. It could mean the IDE or the compiler or any other involved component encountered and handled an error and in the process the debugger is notified and the exception is being reported to the immediate window. This is an excellent post on the topic:

http://blogs.msdn.com/davidklinems/archive/2005/07/12/438061.aspx




回答5:


A quick and easy solution for debug and diag of First Chance Exception is :

AppDomain.CurrentDomain.FirstChanceException += CurrentDomainOnFirstChanceException;

and then

private void CurrentDomainOnFirstChanceException(object sender, FirstChanceExceptionEventArgs firstChanceExceptionEventArgs)
    {
        if (firstChanceExceptionEventArgs.Exception is NullReferenceException)
        {
            // do your handling and debugging :)
        }
    }

Multiple First Chance Exception during the runtime can cripple the performance of your application because exception handling is expensive. Especially in web apps. You can add this handler and look at specific first chance exceptions and try to avoid/correct them.



来源:https://stackoverflow.com/questions/680213/a-first-chance-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!