Skip to content

refactor: rethrow exception without losing stack trace #96

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

Merged
merged 2 commits into from
May 9, 2023

Conversation

cnblogs-dudu
Copy link
Contributor

No description provided.

@ikesnowy ikesnowy self-requested a review May 8, 2023 02:41
@ikesnowy
Copy link
Contributor

ikesnowy commented May 8, 2023

修复一下 Conflict

@ikesnowy
Copy link
Contributor

ikesnowy commented May 8, 2023

搜了一下可以用 AggregateException 来集中抛出异常。

protected void OnMyEvent()
{
    EventHandler handler = MyEvent;
    if (handler != null)
    {
        List<Exception> exceptions = null;
        foreach (var d in handler.GetInvocationList())
        {
            try
            {
                ((EventHandler)d)(this, EventArgs.Empty);
            }
            catch (Exception exc)
            {
                if (exceptions == null)
                    exceptions = new List<Exception>();
                exceptions.Add(exc);
            }
        }

        if (exceptions != null)
            throw new AggregateException(exceptions);
    }
}

https://learn.microsoft.com/en-us/archive/msdn-magazine/2009/brownfield/aggregating-exceptions#figure-2-using-aggregateexception-when-raising-events

@cnblogs-dudu
Copy link
Contributor Author

就应该用 AggregateException

@ikesnowy ikesnowy added this pull request to the merge queue May 9, 2023
Merged via the queue into main with commit fbd175a May 9, 2023
@ikesnowy ikesnowy deleted the use-exceptiondispatchinfo branch May 9, 2023 03:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants