Pattern for reporting exception and continuing
fostandy
Posts: 9
There are some circumstances when we want to catch an exception and do some other handling before dispatching it to our custom handler (which brings up a custom form and sends the report).
In addition, I don't want to bring our program to a crashing halt (we are able to gracefully recover from this exception).
But my question is what is the best way to relay whether or not I want to continue to the handler? (Ideally, I guess I'd like a function like ExceptionReporting.Report(Exception ex, bool tryToContinue))
The most obvious solution to me is to create a
My catch block in Foo() then becomes
And my OnReportException can interrogate the ReportExceptionEventArgs to see if it is of type CustomException and behave as necessary.
I've tested this, and it works, but it kind of smells to me to use an exception for this sort of message passing*. Just wondering if there is a better way?
(* In addition our smartassembly supported applications actually abstract out the error reporting functionality into a exception library, so referencing CustomException from both our UnhandledExceptionHandler and our exception library is a bit funny - but this is not major)
In addition, I don't want to bring our program to a crashing halt (we are able to gracefully recover from this exception).
private void FooImpl() { throw new Exception("failed"); } public void Foo() { try { FooImpl(); } catch (Exception ex) { DoCleanup(); Log(ex); ExceptionReporting.Report(ex); } }I noticed that in its current inception. Foo() will attempt to terminate the program, which is not what I want. Obviously I can then add code to my OnReportException(ReportExceptionEventArgs e) handler to set e.TryToContinue to true.
But my question is what is the best way to relay whether or not I want to continue to the handler? (Ideally, I guess I'd like a function like ExceptionReporting.Report(Exception ex, bool tryToContinue))
The most obvious solution to me is to create a
public class CustomException : Exception { public CustomException(string message) : base(message) { } public CustomException(string message, Exception innerException) : base(message, innerException) { } protected CustomException(SerializationInfo info, StreamingContext context) : base(info, context) { } public bool TryToContinue { get; set; } public CustomException() { } } }
My catch block in Foo() then becomes
catch (Exception ex) { DoCleanup(); Log(ex); ExceptionReporting.Report(new CustomException("", ex)); }
And my OnReportException can interrogate the ReportExceptionEventArgs to see if it is of type CustomException and behave as necessary.
I've tested this, and it works, but it kind of smells to me to use an exception for this sort of message passing*. Just wondering if there is a better way?
(* In addition our smartassembly supported applications actually abstract out the error reporting functionality into a exception library, so referencing CustomException from both our UnhandledExceptionHandler and our exception library is a bit funny - but this is not major)
Comments
Brian: My question was basically what should I put in my catch (Exception e) {} block to be able to set ReportExceptionEventArgs#TryToContinue = true; ?
I'm not seeing an immediate way to deal with this except either with static variables or wrapping the exception. The latter is the cleanest solution I can think of but it is still not particularly elegant.
This is more of a design question than a technical smartassembly question, although I do want to make sure I'm not missing something obvious in the API.
I don't see how I can report the exception without re-throwing it for SmartAssembly to handle via the OnReportException method. And once rethrown I don't see any way to control the continue parameters short of wrapping it in a customized exception.
My question is, what goes into the /* WHAT GOES HERE? */ that will make calls to Foo() and Foo2() behave as I desire?