How to continue the flow of methods using the OnException (PostSharp) aspect?

I have the following code:

[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");
            }

Using FlowBehavior.Continue completes divide () and returns to the main () method.

+5
source share
2 answers

Remember that the OnException component wraps your code in try / catch, so the code will continue from catch (rather than retroning), and by default the behavior will be returned by default. Do you want to continue where did he throw the exception from? If so, you need to explicitly pass this line to try / catch yourself.

Read more at http://www.sharpcrafters.com/blog/post/Day-6-Your-code-after-PostSharp.aspx .

+4
source

, divide(), ExceptionAspectHandler ( ), OnExceptionAspect.

0

All Articles