The Debug-Exceptions… window is a useful tool that at times saves 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.
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.
private void Function_A()
BLLFunction();//this calls few other functions..
catch (Exception ex)
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.
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.
The User-unhandled option seems bit unclear as of now, and couldn’t put time to find a clear usage scenario now, but thought the above is useful enough to write.
If you like this article..
I have posted this blog entry as article in CodeProject too.