Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Annotating Exceptions with extra information without catching them

Exceptions sometimes occur. When they do, they're logged and later analyzed. The log obviously contains the stack-trace and other global information, but often crucial context is missing. I'd like to annotate an exception with this extra information to facilitate post-mortem debugging.

  • I don't want to try{...}catch{... throw;} since that counts as catching an exception and that makes debugging harder (during development I'd like the app to stop and the debugger to react when the original exception is thrown, and not when the outermost uncaught exception is). First-chance exception handlers aren't a workaround since there are unfortunately too many false positives.
  • I'd like to avoid excessive overhead in the normal, non-exceptional case.

Is there any way to store key pieces of context (e.g. filename being processed or whatever) in an exception in a way that doesn't catch the exception?

like image 905
Eamon Nerbonne Avatar asked Nov 22 '11 16:11

Eamon Nerbonne


1 Answers

I am taking a shot at this building off of Adam's suggestion of Aop. my solution would be Unity rather than postsharp and the only question I would have is whether the exception is being caught inside of invoke, which it likely is...

        public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
    {
        //execute
        var methodReturn = getNext().Invoke(input, getNext);

        //things to do after execution
        if (methodReturn.Exception != null)
            methodReturn.Exception.Data.Add("filename", "name of file");

        return methodReturn;
    }
}
like image 92
Rob Allen Avatar answered Sep 21 '22 22:09

Rob Allen