Should I re-throw exceptions within a method, or is there a better way to handle it?
Deciding which exceptions to handle, rethrow, wrap in other exceptions is a design decision. Some exceptions might be caught to branch to a different flow. Naively catching all exceptions is a bad call. Guidelines for handling and throwing exceptions



Some exceptions should be propagated all the way up, because of the integrity of the application or even your machine cannot be preserved. Be careful when encountering OutOfMemoryException, BadImageFormatException, StackOverflowException.

Would you really want to wrap/hide the aforementioned exceptions in a custom exception?


catch (Exception ex)
{
    // Capture any generic exception and retrhow it as a 'WhException'.
    throw new WhException("An error occurred whilst ..", ex.Message, ex);
}





Keep in mind that detail is lost when you catch and throw ex;. A new stacktrace is created.


catch (WhException ex)
{
    // Capture and rethrow a 'WhException', ensurig that the detail is not lost.
    throw ex;
}



Use throw; to preserve stacktrace.

catch (WhException ex)
{
    throw; // preserves stacktrace
}


If you want to rethrow outside the catch block you could use ExceptionDispatchInfo. Suppose Exception error is declared before the try:

catch (WhException ex)
{
    error = ExceptionDispatchInfo.Capture(ex);
}

error?.Throw(); // preserves stacktrace




Consider throwing these well-known exceptions for common situations:


ArgumentNullException : when a mandatory argument is not provided
ArgumentException: when an argument does not meet the preconditions to run
AggregateException: when packing multiple exceptions together