Avoiding First Chance Exception Messages When the Exception Is Safely Handled

Avoiding first chance exception messages when the exception is safely handled

The point of "first-chance" exceptions is that you're seeing them pre-handler so that you can stop on them during debugging at the point of throwing. A "second-chance" exception is one that has no appropriate handler. Sometimes you want to catch "first-chance" exceptions because it's important to see what's happening when it's being thrown, even if someone is catching it.

There's nothing to be concerned with. This is normal behavior.

A first chance exception

What is happening is the debugger can "see" exceptions as soon as they are raised (hence the "first chance") before any catch block is hit. Any exception which is not handled by a catch block is considered a "second chance" exception and will break normally.

If these exceptions aren't stopping the running of your application because they are unhandled then you are probably OK. Most of the time the exception is handled by code and this isn't a problem. The output is simply Visual Studio letting you know the exceptions were raised.

See the "Avoiding first chance exception messages when the exception is safely handled" question for some methods to reduce this if there are too many to ignore.

Prevent first chance exceptions in Output windows

I can answer this for Visual Studio 2013:

In VS 2013 you can go to the DEBUG menu - Options and Settings... - Debugging - Output Window.
Under "General Output Settings" you find "Exception Messages". Turn it off.

First chance exception error handling causing out of memory with multiple instances with word

You must dispose all COM objects.

Please, see this Disposing of Microsoft.Office.Interop.Word.Application

In your case:

System.Runtime.InteropServices.Marshal.ReleaseComObject(word)

PS: Do it in a try .. finally block, just in case you have other exception.

How to find first chance exceptions from output window

Go to Debug, Exceptions (Ctrl + D, E), and check the types you're interested in.

This dialog tells the debugger to break whenever an exception is thrown, regardless of whether it's caught.

Why am I getting an exception whenever I use the CFileDialog?

First-chance exception messages can be safely ignored. In this case, this is part of Windows API internal implementation. Exceptions are thrown and caught somewhere. You need to turn off only first chance exception break in the debugger.

Find source of Exception shown in VS output window


What can I do to find out which part of my code throws this exception or where it happens. Is there a way to show/visualize this in VS? Debugger options?

In Visual Studio, choose Debug then Exceptions.

In the Exceptions window, tick Thrown on the Common Language Runtime Exceptions row.

Sample Image

Click OK.

Now whenever an exception is thrown, the debugger will break on the offending line. You can also expand the selection to only include the exceptions of interest.

Visual Studio 2017

You can find this under Debug.Windows.Exception Settings

Sample Image

Sample Image

.NET - First chance exception listener for intensive debugging?

Net 4.0 has actually added the AppDomain.FirstChanceException event. It fires before any catch block is executed.

This MSDN article has some examples.

Basically you just add an event handler like this:

    AppDomain.CurrentDomain.FirstChanceException += 
(object source, FirstChanceExceptionEventArgs e) =>
{
Console.WriteLine("FirstChanceException event raised in {0}: {1}",
AppDomain.CurrentDomain.FriendlyName, e.Exception.Message);
};

Need C# to break on Selenium first chance exceptions

If you select DEBUG -> Exceptions, there will be a window with a list of exception types and 2 columns, Thrown and User-unhandled. Make sure the 'Thrown' column is selected for Common Language Runtime Exceptions (if Selenium is clr). This will cause the debugger to break for exceptions that are managed with a try/catch.

edit

You may additionally need to select DEBUG -> Options and Settings and in Debugging -> General, deselect the 'Enable Just My Code'. Be careful though, this will cause symbols for .NET framework source to be downloaded, either immediately, or the next time you start debugging.



Related Topics



Leave a reply



Submit