7

Edit: I have looked at the answers code: NONE of them do what I want (I've checked). It would seem that there is no way to do what I want in native c#. I guess that's not a disaster just a shame given that .NET does support it (see accepted answer).

Thanks all.


I have c# code (part of a test framework that will never be run except under a debugger) like this who's point it to avoid actually catching the exception as that makes debugging the code in the unwound part of the stack a royal pain.

Bool bad = true;
try
{
   MightThrow();
   bad = false;
}
finally
{
   if(bad) DoSomeLoggingOnFailure();

   //// Does not catch!!!! 
   //// exception continues to unwind stack.

   //// Note that re throwing the exception is NOT
   //// the same as not catching it in the first place
}

is their a better way to do this?

A solution would have to behave exactly like that under the debugger with regards to un-caught exceptions. It would have to result in the only one first chance exception and the debugger breaking at the point that the exception was originally thrown, not in a catch block.

Specifically I need the debugger on un-caught exceptions to stop a in side MightThrow.

The following doesn't work because it fails to have the debugger break in the correct place

try { ... } catch { throw; }

And this doesn't work because it loses stack info (and also breaks in the wrong place).

try { ... } catch(Exception e) { throw e; }

I known that in D I could use a scope(failure) block

BCS
  • 75,627
  • 68
  • 187
  • 294
  • change "finally" to "catch" and you're golden – Chris Ballance Feb 10 '09 at 20:14
  • you (BCS) are missing something about some of these answers...for instance, to Martin you're saying you don't want to catch the exception. But his solution is correct...the "throw" line in the catch block rethrows the exception with the original stack trace. – Beska Feb 10 '09 at 20:25
  • @BCS in your comment to "Daniel L" you say "That looses the stack in the debugger and in the object"...but it doesn't. If you use "throw" instead of "throw e", it retains the stack you want. – Beska Feb 10 '09 at 20:26
  • I see the reason BCS is worried; he wants to know the exact location the exception was thrown in the debugger. I would argue against this. There's some little value in being able to know where it occurred during debug, yes, but at the expense of a best practice? No way. – Randolpho Feb 10 '09 at 20:28
  • The only reason for the code is debugging. Outside the debugger, the finally block "should" be removed entirely – BCS Feb 10 '09 at 20:34
  • You say none of the answers do what you want, but mine does. – Brian Feb 10 '09 at 20:44
  • 4
    -1: Every answer in this thread correctly answers the question. The OP insists otherwise, so the question must be poorly worded or the OP hasn't explained what he's trying to accomplish. – Juliet Feb 10 '09 at 20:52
  • throw; by itself is NOT the solution in every circumstance. In my tests the stacktrace was being lost. See my answer for a possible solution. – Adam Lassek Feb 10 '09 at 21:03
  • 1
    If you 'catch' and then 'throw' (rethrow), you see two first-chance exceptions in the debugger. With OP's strategy, you see one. (I think) the goal is 'logging that does not ruin change the debug experience', which is why OP has the right idiom already. – Brian Feb 10 '09 at 21:10
  • @BCS, if I have correctly captured you intention, please consider modifying the question accordingly (possibly even show a screenshot of 'where VS stops' in both cases), and then mark my response as the correct answer. – Brian Feb 10 '09 at 21:13
  • please add to your post, that you know where is a StackTrace and that you know what difference between "throw ex;" and "throw;". without this disclaimer, its really lead to misunderstanding. – Avram Feb 10 '09 at 21:19
  • This is weird. How hard can it be to tell VS to stop at the first 'throw' when debugging, and use "catch { cleanup...; throw; }" to do cleanup and then allow the exception to propagate unchanged? – Daniel Earwicker Feb 10 '09 at 22:18

17 Answers17

35

So, in .NET what you're asking for is theoretically possible, but it's not going to be easy.

CIL actually defines five types of exception handling block! The try, catch and finally ones you're used to in C#, and two others:

  • filter - similar to a catch block but can run arbitrary code to determine whether it wants to handle the error, rather than just matching on type. This block has access to the exception object, and has the same effect on the exception stack trace as a catch block.

  • fault - similar to a finally block, however it is only run when an exception occurs. This block does not have access to the exception object, and has no effect on the exception stack trace (just like a finally block).

filter is available in some .NET languages (e.g. VB.NET, C++/CLI) but is not available in C#, unfortunately. However I don't know of any language, other than CIL, that allows the fault block to be expressed.

Because it can be done in IL means not all is lost, though. In theory you could use Reflection.Emit to dynamically emit a function that has a fault block and then pass the code you want to run in as lambda expressions (i.e. one for the try part, one for the fault part, and so on), however (a) this isn't easy, and (b) I'm unconvinced that this will actually give you a more useful stack trace than you're currently getting.

Sorry the answer isn't a "here's how to do it" type thing, but at least now you know! What you're doing now is probably the best approach IMHO.


Note to those saying that the approach used in the question is 'bad practice', it really isn't. When you implement a catch block you're saying "I need to do something with the exception object when an exception occurs" and when you implement a finally you're saying "I don't need the exception object, but I need to do something before the end of the function".

If what you're actually trying to say is "I don't need the exception object, but I need to do something when an exception occurs" then you're half way between the two, i.e. you want a fault block. As this isn't available in C#, you don't have an ideal option, so you may as well choose the option that is less likely to cause bugs by forgetting to re-throw, and which doesn't corrupt the stack trace.

Greg Beech
  • 133,383
  • 43
  • 204
  • 250
  • So the answer is "you can, sort of" – BCS Feb 10 '09 at 21:30
  • @BCS - pretty much, yeah. The infrastructure is there to do it, but nothing I know of exposes it in a useful way. – Greg Beech Feb 10 '09 at 21:37
  • Is there anything that can be done with 'Fault' that can't be done with the combination of 'Filter' and 'Finally'? It seems to be that using 'Filter' and 'Finally' allows one to know what exception if any is pending when 'Finally' executes (and make it available as an InnerException if something goes wrong in the Finally). IMHO, it would be handy if vb and C# allowed a Finally block to have convenient access to that information (in vb.net, it's a bit awkward). I wonder why C# doesn't allow any useful features from Filter, even if the creators don't want to expose Filter completely? – supercat Jan 06 '11 at 22:39
  • 1
    *"filter... has the same effect on the exception stack trace as a catch block."* The filter expression is evaluated during the "first pass". Only when all filters on the stack are evaluated will the "second pass" begin, in which `finally` and `catch` blocks get executed. It's a key feature of exception filters that they don't have side-effects on the stack. – Daniel Earwicker Jan 13 '11 at 15:08
27

How about this:

try
{
  MightThrow();
}
catch
{
  DoSomethingOnFailure();
  throw; // added based on new information in the original question
}

Really, that's all you did. Finally is for things that must run regardless of whether an exception occurred.

[Edit: Clarification]

Based on the comments you've been mentioning, you want the exception to continue being thrown without modifying its original stack trace. In that case, you want to use the unadorned throw that I've added. This will allow the exception to continue up the stack and still allow you to handle part of the exception. Typical cases might be to close network connections or files.

[Second edit: Regarding your clarification]

Specifically I need the debugger on un-caught exceptions to stop at the original point of the throw (in MightThrow) not in the catch block.

I would argue against ever breaking a best-practice (and yes, this is a best-practice for partially handling exceptions) to add some minor value to your debugging. You can easily inspect the exception to determine the location of the exception throw.

[Final edit: You have your answer]

kronoz has thoughtfully provided you with the answer you sought. Don't break best practices -- use Visual Studio properly! You can set Visual Studio to break exactly when an exception is thrown. Here's official info on the subject.

I was actually unaware of the feature, so go give him the accepted answer. But please, don't go trying to handle exceptions in some funky way just to give yourself a hand debugging. All you do is open yourself up to more bugs.

Community
  • 1
  • 1
Randolpho
  • 55,384
  • 17
  • 145
  • 179
  • that catches the exception, finally does not – BCS Feb 10 '09 at 20:14
  • Oh, I see. I'll modify, then. – Randolpho Feb 10 '09 at 20:15
  • The location of the throw is about 10% of the info I want. The rest is the local vars in all those stack frames. Also I'm doing logging, not error recovery. – BCS Feb 10 '09 at 20:31
  • You could, I dunno... get the location from the stack trace and then, say, set a breakpoint at that location? – Randolpho Feb 10 '09 at 20:47
  • He just said he's logging this, not debugging. Also, the stacktrace can still be lost in a throw; depending on what kind of exception. In my test I threw a division by zero error, and it was lost on re-throw. – Adam Lassek Feb 10 '09 at 21:09
  • I +1ed kronoz for his answer but that has the unfortunate side effect of breaking regardless of *any* try/catch/finally blocks. It's to big a hammer. I gave Brian the Green Check as it seems the correct answer turns out to be a simple "NO" – BCS Feb 10 '09 at 21:23
  • It's not that hard to switch. You're breaking standards for a very niche benefit. – Randolpho Feb 10 '09 at 21:30
  • BCS - I added some extra details, you can fine-tune fc-exception catching to a certain degree, though it still may not be the ideal solution for you. I do recommend at least experimenting with them however as they can be very useful! :) – ljs Feb 10 '09 at 21:38
  • @Randolpho - I'd be intrigued to see some referenced articles for these 'best practices' and 'standards' you keep talking about. IMHO, there aren't any, and the approach proposed in the question is fine, and I've explained why in my answer. – Greg Beech Feb 11 '09 at 00:52
  • Let's start with this one: http://msdn.microsoft.com/en-us/library/ms173162.aspx and I'll let you Google the rest. – Randolpho Feb 11 '09 at 01:08
11

If you're interested in the debugger simply stopping precisely where the exception occurred then have you considered first-chance exceptions?

If you open Tools|Exceptions then tick the Common Language Runtime Exceptions box, the debugger will stop at the point of exception regardless of any try/catch/finally blocks.

Update: You can specify the precise exception you wish to catch by expanding the [+] tree in the Exceptions dialog. Though of course it will fire every time any exception of the specified type[s] occur[s], you can switch it on and off at will even in the middle of a debugging session, so with judicious use of breakpoints you can get it to do your bidding. I used it successfully to get around the 'target of an invocation has thrown an exception' ball ache originating from using reflection to instantiate objects. Very useful tool in such circumstances. Also note the locals and stack trace should be firmly available as far as I recall (just did a quick test and they are available), so no problems there.

Of course if you want to log things then that is outside the scope of an IDE debugger; and in which case first-chance exceptions won't help you!

Give it a go at least; I found them very useful and they might be more appropriate for your issue than you think.

ljs
  • 37,275
  • 36
  • 106
  • 124
  • I can't figure out a way to code it the way he wants to - you're right, the IDE has a way to do this already. – Knobloch Feb 10 '09 at 20:59
  • Maybe he's not happy that first chance stops at MightThrow() instead of some line inside MightThrow()?? – Knobloch Feb 10 '09 at 21:01
  • It'll stop at the line inside MightThrow(). Unless it's in some proprietary 3rd party library, in which case he's asking the impossible! – ljs Feb 10 '09 at 21:04
  • The impossible would be nice, but I can live with the merely difficult :) – BCS Feb 10 '09 at 21:12
  • Since my post is currently at the top of the list, I linked to your answer there. BCS should mark this as the accepted answer. – Randolpho Feb 10 '09 at 21:14
  • This answer is the best /practical/ solution. However the "fault block" solution is exactly the info I was looking for. I'd accept both if it were possible. – BCS Feb 10 '09 at 23:36
7

What's wrong with:

try
{
   MightThrow();
}
catch
{
   DoSomthingOnFailure();
   throw;
}
Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
7

For code that should only run on exceptions, use the catch block:

try
{
   MightThrow();
}
catch (Exception ex)
{
   // this runs only when there was an exception
   DoSomthingOnFailure();
   // pass exception on to caller
   throw; 
}
finally
{
   // this runs everytime
   Cleanup();
}
M4N
  • 94,805
  • 45
  • 217
  • 260
  • that catches the exception, finally does not and I don't want to. see edit. – BCS Feb 10 '09 at 20:15
  • @BCS: Yes...it "catches" the exception, but it's doing what you want because of the "throw" line in the catch block...which rethrows the exception with the original stack trace. – Beska Feb 10 '09 at 20:23
  • but the debugger will then break on the throw; line so it does not do what I want. – BCS Feb 10 '09 at 20:28
4

This is what you want. It will only call this method when an error occurs, and the "throw" statement will re-throw the exception with the callstack intact.

try
{
   MightThrow();
}
catch
{
   DoSomthingOnFailure();
   throw;
}
Jon Tackabury
  • 47,710
  • 52
  • 130
  • 168
3

A "finally" block that runs only on failure is called "catch" (with no parameters). :-)

Now, there is a small caveat. If you want to have a specialised "catch" case for a particular exception type and have a generic "catch" that works for all exceptions, you'll have to do a bit of a custom logic.

Thus, I would do something like:

  try
  {
    MightThrow();
  }
  catch(MyException ex)
  {
    // Runs on MyException
    MySpecificFailureHandler()
    // Since we have handled the exception and can't execute the generic
    // "catch" block below, we need to explicitly run the generic failure handler
    MyGenericFailureHandler()
  }
  catch
  {
    // Runs on any exception hot handled specifically before
    MyGenericFailureHandler()
    // If you want to mimic "finally" behavior and propagate the exception
    // up the call stack
    throw;
  }
  finally
  {
    // Runs on any failure or success
    MyGenericCleanupHandler();
  }
Franci Penov
  • 74,861
  • 18
  • 132
  • 169
  • a catch block catches the exception, a finally block does not. That difference the the only thing I'm interested in. – BCS Feb 10 '09 at 20:26
  • Technically, the "finally" block also "catches" the exception, as it is invoked when the exception occures. In the case of finally though, any exeption is implicitly rethrown, and for the catch you have to do it explicitly. Hence, the parameterless "throw", which generates "rethrow" IL instruction. – Franci Penov Feb 10 '09 at 20:34
  • the debugger breaks before the finally or after the catch. – BCS Feb 10 '09 at 20:35
  • That is correct. However, the exception still has the proper stack trace in it. I would strongly advise against writing code that tailors to the behavior of a particular debugger. It would be extremely easy to forget to set "bad" in your try block and get side effects due to the "finally" block. – Franci Penov Feb 10 '09 at 20:45
  • @Franci - turns out that finally does not catch the exception. It is technically completely different to a catch. – Daniel Earwicker Mar 02 '09 at 12:06
2

Every example so far is losing the original StackTrace according to my tests. Here's a solution that should work for you.

private static void PreserveStackTrace(Exception exception)
{
  MethodInfo preserveStackTrace = typeof(Exception).GetMethod("InternalPreserveStackTrace",
    BindingFlags.Instance | BindingFlags.NonPublic);
  preserveStackTrace.Invoke(exception, null);
}

try
{
   MightThrow();
}
catch (Exception ex)
{
    DoSomethingOnFailure();
    PreserveStackTrace(ex);
    throw;
}
Adam Lassek
  • 35,156
  • 14
  • 91
  • 107
  • I rather suspect that it will still break in the wrong place (OTOH what you do solve is an issue I have wanted to solve in other places) +1 – BCS Feb 10 '09 at 21:10
  • In that case I think you still need to clarify what problem you're trying to solve. – Adam Lassek Feb 10 '09 at 21:18
2

How about only catching an exception that "MightThrow" does not throw?

Bool bad = true;
try
{
   MightThrow();
   bad = false;
}
catch (SomePrivateMadeUpException foo)
{ 
   //empty
}
finally
{
   if(bad) DoSomeLoggingOnFailure();   
}
jdisk
  • 484
  • 3
  • 10
  • What's the point in that? Why bother with the catch block at all? You don't need one. – Greg Beech Feb 10 '09 at 21:24
  • Well,as far as I know C# insists on having at least one catch after the try block. (See http://msdn.microsoft.com/en-us/library/0yd65esw.aspx ) This is contrary to Java, where the catch part is optional. C# – jdisk Feb 11 '09 at 21:18
  • 3
    the catch block is optional in C#, http://msdn.microsoft.com/en-us/library/s7fekhdy.aspx – Lucas Feb 13 '09 at 22:14
2

Let me recap your requirements the way I understand them:

  1. You want some code that is run only when an exception is generated, in order to do some logging.
  2. You want to run your test framework under debugger and break at the point at which the exception is thrown.

To meet your first requirement, you should write the code the way everybody suggested - using parameterless catch and throw.

To meet your second requirement while using the parameterless catch, you could configure your debugger to break when an exception is throw, not only when there's an unhandled exception. I suspect you know how to do it, but I'll put it here for answer completeness: in VS you can do that in Debug -> Exception -> Common Language Runtime Exceptions -> check the Thrown checkbox.

If you know that your app throws a lot of handled exceptions, that might not be an option for you. At that point, your only choice left to meet your first requirement is to either write the code to use finally for exception logging purposes or look into the direct IL emitting route as Greg Beech suggests.

However, whether the finally code is being executed depends on the debugger you are using. In particular, VS will break on an unhadled exception before the finally is executed and will not let you continue. Thus, unless you detach from the process at that point, your logging code will never be executed. In other words, the second requirement will interfere with meeting the first requirement.

Franci Penov
  • 74,861
  • 18
  • 132
  • 169
  • this code may well run inside of another catch block some times and not others. – BCS Feb 10 '09 at 23:26
2

You could encapsulate your logic in a custom class, something like:

    public  class Executor
{
    private readonly Action mainActionDelegate;
    private readonly Action onFaultDelegate;

    public Executor(Action mainAction, Action onFault)
    {
        mainActionDelegate = mainAction;
        onFaultDelegate = onFault;
    }

    public  void Run()
    {
        bool bad = true;
        try
        {
            mainActionDelegate();
            bad = false;
        }
        finally
        {
            if(bad)
            {
                onFaultDelegate();
            }
        }
    }

}

And use it as:

            new Executor(MightThrow, DoSomeLoggingOnFailure).Run();

Hope this helps.

Jaime Febres
  • 1,267
  • 1
  • 10
  • 15
1

You could write, or have someone write for you, a small assembly in VB.net which implements a TryFaultCatchFinally(of T) method that accepts four delegates:

  1. TryMethod -- An Action(of T) to perform the "Try" block.
  2. FaultMethod -- A Predicate(Of T, Exception) which, if an exception occurs, will be called before any "finally" blocks run; if it returns true the Catch block will run--otherwise it won't.
  3. CatchMethod -- An Action(Of T, Exception) to be performed if an exception had occurred and FaultMethod returned true; happens after "finally" blocks run.
  4. FinallyMethod -- An Action(OF T, Exception, Boolean) to be performed as a "Finally" block. The passed-in exception will be null if TryMethod ran to completion, or will hold the exception that caused it to exit. The Boolean will be true if the exception was caught, or false otherwise.

Note that when the FaultMethod is executed, one may be able to examine the state of objects that caused the exception, before such state is destroyed by Finally blocks. One must use some care when doing this (any locks that were held when the exception was thrown will continue to be held) but the ability may still sometimes be handy, especially when debugging.

I'd suggest the routine look something like:

    Shared Sub TryFaultCatchFinally(Of T)(ByVal TryProc As Action(Of T), _
                                          ByVal FaultProc As Func(Of T, Exception, Boolean), _
                                          ByVal CatchProc As Action(Of T, Exception), _
                                          ByVal FinallyProc As Action(Of T, Exception, Boolean), _
                                          ByVal Value As T)
        Dim theException As Exception = Nothing
        Dim exceptionCaught As Boolean = False
        Try
            TryProc(Value)
            theException = Nothing
            exceptionCaught = False
        Catch Ex As Exception When CopyExceptionAndReturnFalse(Ex, theException) OrElse FaultProc(Value, Ex)
            exceptionCaught = True
            CatchProc(Value, Ex)
        Finally
            FinallyProc(Value, theException, exceptionCaught)
        End Try
    End Sub
supercat
  • 77,689
  • 9
  • 166
  • 211
1

Isn't this the same as:

try 
{
    MightThrow();
}
catch (Exception e) 
{
    DoSomethingOnFailure();
    throw e;
}

?

Dan Vinton
  • 26,401
  • 9
  • 37
  • 79
  • No... you lose the stack info when you throw the object. Better to just use throw since stack preservation is needed. – StingyJack Feb 10 '09 at 20:27
  • @StingyJack - Okay, you lose the stack info, but (other than for debugging/logging) you're not doing anything with that, right? Otherwise you're coupling your error handling to your stack... – Dan Vinton Feb 10 '09 at 20:51
  • As it happens, the code is /only/ debugging and logging code. – BCS Feb 10 '09 at 21:13
  • Ah... you may be stuck with the 'bad' flag then, unless you can debug/log in the catch block? – Dan Vinton Feb 10 '09 at 22:02
0

Have you considered using the DebuggerStepThrough attribute? http://msdn.microsoft.com/en-us/library/system.diagnostics.debuggerstepthroughattribute.aspx

[DebuggerStepThrough]
internal void MyHelper(Action someCallback)
{
    try
    {
        someCallback();
    }
    catch(Exception ex)
    {
        // Debugger will not break here
        // because of the DebuggerStepThrough attribute
        DoSomething(ex);
        throw;
    }
}
Ron Jacobs
  • 3,279
  • 2
  • 18
  • 16
0

No, I think this is a common idiom the way you have it.

EDIT To be clear, the "catch" then "rethrow" strategies offer the same run-time semantics, however they change the experience when the VS debugger is attached. Tooling and maintenance is important; debugging often requires you to 'catch all first-chance exceptions' and if you end up with lots of 'spurious' first-chance exceptions due to catch-then-rethrow in your code, it really hurts the ability to debug the code. This idiom is about interacting well with the tooling, as well as clearly expressing the intent (you don't want to 'catch', decide can't handle, and rethrow, instead you just want to log that an exception did happen but let it pass on by).

Brian
  • 117,631
  • 17
  • 236
  • 300
0

With exception filters added in C# 6, one option is to make use of a false returning exception filter, like so:

void PerformMightThrowWithExceptionLogging()
{
    try
    {
        MightThrow();
    }
    catch (Exception e) when (Log(e))
    {
        // Cannot enter here, since Log returns false.
    }
}

bool Log(Exception e)
{
   DoSomeLoggingOnFailure(e);
   // Return false so the exception filter is not matched, and therefore the stack is kept.
   // This means the debugger breaks where the exception actually happened, etc.
   return false;
}

See https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch for more details on exception filters.

Andrew McClement
  • 1,171
  • 5
  • 14
-1
try
{
   MightThrow();
}
catch
{
   DoSomethingOnFailure();
}
Andy Mikula
  • 16,796
  • 4
  • 32
  • 39