Annotating exceptions with additional information without catching them

Sometimes exceptions arise. When they do, they are recorded and then analyzed. The log obviously contains stack traces and other global information, but often lacks key context. I would like to annotate an exception with this additional information in order to facilitate post-mortem debugging.

  • I donโ€™t want to try{...}catch{... throw;} , since this is considered catching an exception, which complicates debugging (during development, I would like the application to stop and the debugger to respond when the original exception is thrown, and not when the most external uncaught exception). First chance exception handlers are not a workaround, because, unfortunately, there are too many false positives.
  • I would like to avoid excessive overhead in a normal, not exceptional case.

Is there a way to save key fragments of the context (for example, the processed file name or something else) in the exception, so as to exclude the exception?

+6
source share
3 answers

Iโ€™m renting Aopโ€™s offer in this building with Adam. my solution will be Unity, not postharp, and the only question I will have is whether the exception will fall within invoke, which is probably ...

  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; } } 
+1
source

There is nothing wrong with the following pattern:

 try { // Do Something } catch (GeneralException ex) { throw new SpecificException( String.Format("More specifics ({0}) in message", someData), moreContext, new {even, more, context}, ex); } 

This is exactly the template that should be used, for example, when "Do Something", say, opens some kind of file. "SpecificException" may be "unable to read configuration file".

+1
source

I would not use AOP to catch exceptions. Instead, I would use a catch class for the LOG exception + all arguments to the method that threw the exception. And then let the original exception be thrown again

0
source

Source: https://habr.com/ru/post/902126/


All Articles