Click here to Skip to main content
15,886,720 members
Articles / Web Development / ASP.NET

The {not much utilized} Debug->Exceptions… Window Technique

Rate me:
Please Sign up or sign in to vote.
3.67/5 (7 votes)
11 Feb 2010CPOL2 min read 12.3K   7   4
The not much utilized Debug->Exceptions... Window technique

The Debug-Exceptions… window is a useful tool that at times saves a lot of time when debugging. Even though this exists (I think) from Visual Studio 2003, I still find people struggle a bit with the situation I am going to explain next.

As you know, Visual Studio highlights the exception code line when it is not handled. Sometimes, we purposely don't handle exceptions in a method and let the exception thrown to calling functions.

filenotfound1

Usually, we have a multi-project solution that typically has layers like Business logic, Data access layer, UI, etc. Imagine that you make a call from the UI to a local function and it calls another function in BLL and the call goes deep into layers of your project architecture.

Now, when it is not sure if each of the methods getting called handles exceptions in it or doesn't throw identifiable exceptions.

For instance:

C#
private void Function_A()
{
    try
    {
        BLLFunction();//this calls few other functions..
    }
    catch (Exception ex)
    {
        //handle exception
    }
}

Function_A() calls BLLFunction() and it again calls few other functions, but I have handled exceptions only in Function_A(). In this case, whatever method in the call hierarchy triggers an exception, the Visual Studio debugger takes the control to the catch() in Function_A(). Debugging this could be bit hard when the number of functions is more and code is complex. You have to narrow down to the function and line the exception occurred.

Instead, a simple technique can be performed with the Debug-Exceptions window...

If you choose Debug –> Exceptions…, a window appears with various categories of exceptions. Check the Thrown check box beside Common Language Runtime Exceptions, and now if you re-debug the project, the line highlighted by debugger will be the exact line where the exception occurs in the exception triggering method.

debug-exceptions

What happens is, we specify that all Common Language Exceptions are Thrown regardless of whether it is handled or not.

You can even set only the exception you want to be thrown by selecting the exception using the Find.. option as below, this will only throw that specific exception even if it is handled elsewhere and ignore other exceptions.

debug-exceptions1

The User-unhandled option seems a bit unclear as of now. I couldn't put time to find a clear usage scenario now, but thought the above is useful enough to write.

License

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


Written By
Architect
India India
I am Ashoka (Ashoka.R.K.T.), I am a software professional who is passionate about designing and developing great applications. I blog at https://ashokaon.tech, I will be writing about all things technology and coding.

Comments and Discussions

 
Generalsimple, yet useful Pin
bigfoot10417-Feb-10 5:05
bigfoot10417-Feb-10 5:05 
GeneralGreat Work Pin
Bilal Haider17-Feb-10 2:25
professionalBilal Haider17-Feb-10 2:25 
GeneralMy vote of 1 Pin
Gavin Harriss16-Feb-10 20:22
Gavin Harriss16-Feb-10 20:22 
Short article that doesn't really provide much value.
GeneralUser unhandled Pin
TrendyTim15-Feb-10 18:36
TrendyTim15-Feb-10 18:36 

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.