Sphyrnidae Common Library  2.0.1
Shared Utilities/Library
Safe Try

Overview

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:

  1. Action: returns boolean for success (eg. false if exception was thrown)
  2. Func<T>: returns the object, or default value on exception
  3. Func<Task>: Same as Action, but is awaitable
  4. Func<Task<T>>: Safe as Func<T>, but is awaitable

Performance Concerns

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.

Where Used

  1. IgnoreException:
    1. Get Variable Error
    2. Getting settings error
    3. Distributed Cache interaction
    4. Logging as a catch-all around EmailException handling
    5. RegExAttribute
    6. HealthCheckOptions
    7. JWT parsing
    8. IpAddress
    9. NameValueCollectionExtensions
    10. Setting of Logging information
    11. Getting most of Request Data properties
    12. EmailException (on exception catch)
    13. Capturing logging update information
  2. EmailException:
    1. SignalR connection issues and logging
    2. Logging issues
    3. Get User Preference Error
    4. Get Feature Toggle Error
  3. LogException:
    1. SignalR Cache Invalidation Receive Registration
    2. Create/Update User Preference Error

Examples

    // "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");