首页 > 解决方案 > 重新抛出异常 Fody 时应用程序中断

问题描述

我正在尝试处理从异步方法抛出的所有异常,但是当我在执行异步子方法后抛出异常时,然后从 fody(OnException 方法)应用程序中断重新抛出异常。

喜欢:

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)
        {
        }
    }
}

标签: c#async-awaitfody

解决方案


推荐阅读