0

I am trying to handle all the exceptions thrown from a async method, but when i throw the exception after the execution of an async child method,then on re-throw exception from fody(OnException method) application breaks.

Like:

using MethodBoundaryAspect.Fody.Attributes;

public sealed class ExceptionHandlerAttribute : OnMethodBoundaryAspect
{

    public override void OnException(MethodExecutionArgs args)
    {
        //re-throw exception
        throw args.Exception;
    }
}

public class ClassName
{
    [ExceptionHandler]
    public async Task<T> MethodAsync()
    {
        //do some async work
        await OtherClass.DoSomeWorkAsync();
        throw new Exception("Exception after the execution of an Async method");
    }
}

public class ParentClass
{
    public async Task<T> MethodAsync()
    {
        try
        {
            ClassName className=new ClassName();
            await className.MethodAsync();
        }
        catch(Exception ex)
        {
        }
    }
}
9
  • 1
    How does the application break? Commented Sep 1, 2020 at 19:39
  • @PauloMorgado execute the above code in a WEB application. Commented Sep 1, 2020 at 19:51
  • So, who is catching the exception, or who do you expect to catch it? Are you sure that OnException should re-throw?
    – Andrew
    Commented Sep 1, 2020 at 20:01
  • @Andrew i have just added some more detail in code kindly recheck it. Commented Sep 1, 2020 at 20:13
  • Just run it outside the debugger. Then it won't break. Commented Sep 1, 2020 at 20:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.