Breakpoint on exit of function due to exception?

I have been looking through code that some of my collegues have written, and I have found many instances in their code where they do the following:

public void SomeFunction()
{
try
{
}
catch(Exception ex)
{
throw ex;
}
}

When I ask them why they've done this, they tell me that its because they want to be able to put a breakpoint on the line "throw ex", so they can see if a particular function is throwing an exception. The problem is that this code goes into production, and I know that try blocks have performance overhead.

Is there anyway to put a conditional breakpoint in a method, perhaps on the closing brace, such that it only stops if the function is exited due to an uncaught exception


Answer this question

Breakpoint on exit of function due to exception?

  • BobH2

    Unlike C++ and some other languages .NET exceptions have no overhead until you actually throw an exception. Creating exception objects and using try-catch take no time (except adding an element onto the stack I believe). So performance isn't a concern here.

    I have several concerns. The first concern is that you shouldn't use try-catch in order to use breakpoints. I'd recommend that all such code be removed without reservation. The second concern is that you almost never catch all exceptions anyway. If you're running code analysis then the analyzer will pick this off. Thirdly by using throw ex you are actually changing the exception being thrown. When the throw is hit the exception call stack will point to the person who threw the exception. After throw ex is called the exception call stack is changed to now point to the throw ex line. Thus you've lost the original call stack. To rethrow an exception you should simply use throw with no exception object.

    Finally, if you are running in the debugger then as soon as throw is called the debugger can be told to break in through the Exceptions dialog. By default only unhandled exceptions will trigger a break into the code. You can change this on a per-exception basis or for all exceptions.

    Michael Taylor - 2/9/07
    http://p3net.mvps.org


  • adimasi

    If u really want to do tat then u can do one thing u can put whole function in Try Catch Block . .

    by tat way u can catch Exception at end of Functionn



  • olap_user

    Good to know that try catch doesn't have any overhead. I guess I'm still stuck in my C++ days.

    I know that:

    try { } catch(Exception ex) { throw ex; }

    should really be

    try { } catch(Exception ex) { throw; };

    Thanks for the info about the "Exceptions" dialog. Never knew about that. It doesn't answer my problem though. The Exceptions dialog does not allow you to pick the functions which you want to break on, just the type of execption.

    For instance lets say you have:

    class A
    {
    void MethodB
    {
    // Throws an exception
    }


    void MethodC
    {

    // Throws an exception
    }

    }

    Is there a way to set a break point on MethodC which breaks on the closing brace if MethodC throws an exception which is not caught in MethodC


  • just david

    Then u can keep whole try catch block inside ur class wrapping ur whole

    code .. n end it up at closing braces ..

    U cant get a Perfect way to do it but u can go the round way



  • Cappy Popp

    The short answer is no. Conditional breakpoints and tracepoints allow you to do some conditional work but none of them really let you trap on an exception. The problem is that, unlike normal execution, if an exception is thrown and not handled the call stack is torn down after finding the handler. No code is called so your function just disappears. There really is no place to set a breakpoint.

    I don't know what benefit you'd gain by knowing that execution left your function by throwing an exception. What does it matter whether your function was invoked when an exception was thrown or not Generally if an exception is thrown all you care about is who threw it and the call stack leading up to it. In that case the Exception dialog will help you.

    Michael Taylor - 2/9/07
    http://p3net.mvps.org


  • Krenshau75

    Actually what I don't want to do it wrap the code in an empty try catch block.
  • Breakpoint on exit of function due to exception?