![]() |
Sphyrnidae Common Library
2.0.1
Shared Utilities/Library
|
The SafeTry static class methods are basically syntatic replacements for the try/catch block. The method that is passed to this call will be executed inside the 'try'. Depending on the static method that is called, the 'catch' will do various things:
Method | Catch Handling |
---|---|
IgnoreException | Debug.WriteLine() will be called with the exception information |
EmailException | An Email of type HiddenException will be sent containing the exception information. Note that the generation of the subject, body, and sending of the Email will all have exceptions handled using IgnoreException |
LogException | The exception will be sent to the logger as a HiddenException |
OnException | User-supplied handling method |
There are various overloads on all methods to work with basically any return type:
The usage of this class introduces some overhead. First, it takes what was in a single method, and puts that method as a sub-method. So this adds another layer to the call stack. Additionally, all of the state/binding must now occur. Variables used by the internal method now must be "captured" and scope created. If you are using the asynchronous methods (inner method returns a Task), then another layer of state management must be created.
All of this is fairly small in scope, but this could add up depending on the complexity of your application. If you are seeing performance concerns related to this increased call stack, you can always revert back to the standard try/catch notation. You can, however, utilize the Exception handling method in your catch block.
// "Standard" way of doing things string val; try { // Do lots of stuff val = "value"; } catch (Exception ex) { // Ignore, Email(), Log(), Other val = "error happened"; } // Using SafeTry var val = SafeTry.IgnoreException(() => { // Do lots of stuff return "value"; }, "error happened");