Passing Caught C# Exception as Parameter

54 views Asked by At

Will Jon Skeet feed me to his pet unicorn if I do something like the following?

private void TakeTheRisk()
{
    try
    {
        ...
        DoRiskyBusiness();
        ...
    }
    catch (SomeException ex)
    {
        ProcessAndRethrowException(ex);
    }
}

private async Task TakeYetAnotherRiskAsync()
{
    try
    {
        ...
        await DoEvenMoreRiskyBusinessAsync();
        ...
    }
    catch (SomeException ex)
    {
        ProcessAndRethrowException(ex);
    }
}

private void ProcessAndRethrowException(SomeException ex)
{
    ...
    throw; // given to understand `throw ex;` will lose stack trace, right?
}

Main motivation is to commonize exception processing logic. This processing logic is non-trivial and I would prefer to not need to duplicate it. Is it possible to somehow refactor it out of TakeTheRisk and TakeYetAnotherRiskAsnyc into ProcessAndRethrowException as above?

0

There are 0 answers