Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider usage of ExceptionDispatchInfo instead of BubbleExceptionContainer #124

Closed
jl0pd opened this issue Nov 4, 2021 · 1 comment · Fixed by #141
Closed

Consider usage of ExceptionDispatchInfo instead of BubbleExceptionContainer #124

jl0pd opened this issue Nov 4, 2021 · 1 comment · Fixed by #141

Comments

@jl0pd
Copy link
Contributor

jl0pd commented Nov 4, 2021

Current implementation with BubbleExceptionContainer have issue of changing original stacktrace.

Consider following code. Semantically it's identical to current behavior - if exception is throw, store it to some field and throw it elsewhere.

void ThrowSomeException() => throw new InvalidOperationException();

void StoreAndThrowExample()
{
    try
    {
        try
        {
            ThrowSomeException();
        }
        catch (Exception ex)
        {
            var storedEx = ex;
            throw ex;
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}

Result of invoking StoreAndThrowExample is

System.InvalidOperationException: Operation is not valid due to the current state of the object.
   at Program.<<Main>$>g__StoreAndThrowExample|0_2()

Notice that original source is not listed in stacktrace.

Now look at this method that uses ExceptionDispatchInfo:

void ExceptionDispatchInfoExample()
{
    try
    {
        try
        {
            ThrowSomeException();
        }
        catch (Exception ex)
        {
            var exInfo = ExceptionDispatchInfo.Capture(ex);
            exInfo.Throw();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}

which produces following stacktrace:

System.InvalidOperationException: Operation is not valid due to the current state of the object.
   at Program.<<Main>$>g__ThrowSomeException|0_0()
   at Program.<<Main>$>g__ExceptionDispatchInfoExample|0_1()
--- End of stack trace from previous location ---
   at Program.<<Main>$>g__ExceptionDispatchInfoExample|0_1()

This approach is used in async-await

@codingseb
Copy link
Owner

Hello @jl0pd. Thanks for your issue.

I didn't know ExceptionDispatchInfo.
I will look how to use it in ExpressionEvaluator as soon as I have some time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants