我有以下代码:
[Serializable]
class ExceptionAspectHandler:OnExceptionAspect
{
public override void OnException(MethodExecutionArgs args)
{
Console.WriteLine("{0}", args.Exception);
args.FlowBehavior = FlowBehavior.Continue;
}
}
[OnExceptionAspect]
public static void divide()
{
int n = Convert.ToInt32(Console.ReadLine());
var a = 100 / n; //the exception happens here
Console.WriteLine("it should get here");
}
使用 FlowBehavior.Continue 结束 divide() 并返回到 main() 方法。
最佳答案
请记住,OnException 方面将您的代码包装在 try/catch 中,因此代码将从捕获继续(而不是重新抛出)并且其行为将默认返回。您是否希望它从抛出异常的地方继续?如果是这样,您需要自己在 try/catch 中显式包装该行。
请阅读http://www.sharpcrafters.com/blog/post/Day-6-Your-code-after-PostSharp.aspx了解更多详情。
关于c# - 如何使用 OnException 方面(PostSharp)继续方法流程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10553088/