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

[Proposal]: async method exception filter #4485

Open
4 tasks
jcouv opened this issue Mar 1, 2021 · 2 comments
Open
4 tasks

[Proposal]: async method exception filter #4485

jcouv opened this issue Mar 1, 2021 · 2 comments

Comments

@jcouv
Copy link
Member

jcouv commented Mar 1, 2021

async method exception filter

  • Proposed
  • Prototype: Not Started
  • Implementation: Not Started
  • Specification: Not Started

Summary

Allow customizing the exception handling produced for async methods.
Adding the following attribute:
[module: AsyncExceptionFilter(typeof(MyExceptionHandler)]
would result in the generated try/catch having a modified catch clause:

catch (Exception e) when (MyExceptionHandler.Report(e); true)
{
    ... existing handling logic ...
}

where we could find the following method void Report(Exception).

Motivation

There's exception handling that is better done inside the async method so that it can see the original stacktrace and can produce useful crash dumps, but it can be painful to repeat in every async method.
The roslyn IDE code is an example, where there is desire to add the following pattern to every async method:

private async Task ExampleAsync()
{
    try
    {
        ... async code ...
    }
    catch (Exception e) when (FatalError.ReportAndPropagateUnlessCanceled(e))
    {
        throw ExceptionUtilities.Unreachable;
    }
}

Example
FatalError

Design Meetings

@jcouv jcouv added this to the Working Set milestone Mar 1, 2021
@jcouv jcouv self-assigned this Mar 1, 2021
@mburbea
Copy link

mburbea commented Mar 1, 2021

This is something I'd love to have, but I'm curious will this retain the contextual variables?
e.g.

public Task M()
{
    using (_logger.BeginScope("label"))
             await SomeMethodThatThrows();
}

If I wrap this with a logging filter the scope will be lost normally in the async transformation.

public Task M()
{
    using (_logger.BeginScope("label"))
    {
       try{  await SomeMethodThatThrows();}
       catch(Exception e) when (LoggingFilter(_logger, e)){}
    }
}

@HaloFour
Copy link
Contributor

HaloFour commented Mar 2, 2021

Would it be useful to also be able to apply this attribute to specific methods to opt-in to the exception filter where needed instead of it affecting the entire assembly?

@jcouv jcouv moved this to Language/design in Compiler: Julien's umbrellas Jun 3, 2024
@dotnet dotnet locked and limited conversation to collaborators Nov 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Status: Language/design
Development

No branches or pull requests

3 participants