3
votes

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

5
Lol. You live in a multi-timezone world my friend!Jonathan Parker

5 Answers

2
votes

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
votes

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

0
votes

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.

0
votes

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

0
votes

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.