Click here to Skip to main content
15,881,882 members
Articles / Operating Systems / Windows
Article

FIRST CHANCE Exceptions

Rate me:
Please Sign up or sign in to vote.
2.92/5 (19 votes)
19 Jun 2006CPOL2 min read 85.1K   12   5
Are there any sort of exceptions in .NET that cannot be caught by the runtime?

Introduction

Today, while answering few questions on MSDN Newsgroup, I came across one very interesting question and that made me write a brief article about it. The question was "Are there any sort of exceptions that cannot be caught by the runtime?" While answering, I came across another interesting query in which a sender presented a scenario in which he was using a Web Service and at some point the Web Service was raising an exception of type System.Net.WebException. Although he had placed a proper Try Catch block, the application was crashing. Interesting huh!!!

These kind of exceptions are usually called FIRST CHANCE EXCEPTIONS.

What is a FIRST CHANCE Exception?

When you debug an application, 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. Now 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.

In Visual Studio, you may see a message in the output window that looks like this:

A first chance exception of type 'System.ApplicationException' occurred in myapp.exe. 

If you are using Visual Studio .NET 2003, this message is shown if you have configured the debugger to stop when the specific exception type is thrown.

If the application does not handle the exception, the debugger is re-notified. This is known as a "SECOND CHANCE" exception.

The debugger again suspends the application and determines how to handle this exception. Typically, debuggers are configured to stop on second chance (unhandled) exceptions and allow you to debug.

How to Handle First Chance Exceptions?

If you are Using Visual Studio .NET 2003, you can easily configure how the debugger handles first chance exceptions on a per exception basis or for exception groups.

Let me show you how to enable a specific exception to be caught. I will use System.Exception in this example.

  • Start Visual Studio .NET 2003 IDE and load any project
  • Go to Debug menu, select Exceptions
  • Expand the Common Language Runtime Exceptions node in the list
    Expand the System node
  • Select System.Exception in the list
  • If you see below in the list, in the area labelled when the exception is thrown, select Break into the debugger
  • Click OK

Sample screenshot

Typically, the default value for "when the exception is thrown" (a.k.a. first chance exceptions) is "Use Parent Settings (the specific exception will inherit the exception group setting.)".

In the same fashion, you can enable a group of exceptions to be caught in a specific manner.

Note: When enabling for exception groups, specific exception settings override the group level setting.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Arab Emirates United Arab Emirates
Altaf Al-Amin Najvani
Project Manager
Commercial Bank Of Dubai


Qualifications:
BS - Computer Science
Masters In Project Management
Masters In Business Administration

Certifications:
CISA - Certified Information Systems Auditor
ITIL (Foundation)
Microsoft Certified Technology Specialist (SQL Server 2005)
Microsoft Certified Technology Specialist (Web Applications)
Microsoft Certified Application Developer (.NET)
Microsoft Certified Solution Developer (.NET)

Comments and Discussions

 
GeneralMy vote of 5 Pin
jarqlo1-Oct-10 4:14
jarqlo1-Oct-10 4:14 
i was looking for it Smile | :)
GeneralJust what I was looking for! Pin
chaiguy133730-Oct-08 5:24
chaiguy133730-Oct-08 5:24 
QuestionHandling the First Chance Exception?!? Pin
kmillen30-Apr-07 3:13
kmillen30-Apr-07 3:13 
AnswerRe: Handling the First Chance Exception?!? Pin
chaiguy133730-Oct-08 4:14
chaiguy133730-Oct-08 4:14 
AnswerRe: Handling the First Chance Exception?!? Pin
Greg Osborne13-Apr-16 7:00
Greg Osborne13-Apr-16 7:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.