Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

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

3.67/5 (7 votes)
11 Feb 2010CPOL2 min read 12.6K  
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)