6

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 ends divide() and returns to the main() method.

Gustavo Puma
  • 993
  • 12
  • 27
  • Um, your method is decorated with `[OnExceptionAspect]` so it will do the default PostSharp `OnExceptionAspect` behaviour which is nothing. You need to decorate it with `[ExceptionAspectHandler]` to get your code to work – Shevek Jun 19 '15 at 12:01

2 Answers2

5

Remember, the OnException aspect wraps your code in a try/catch so the code will continue from the catch (instead of rethrowing) and it's behavior will default to return. Are you wanting it to continue from where it threw the exception? If so, you need to explicitly wrap that line in a try/catch yourself.

Please read http://www.sharpcrafters.com/blog/post/Day-6-Your-code-after-PostSharp.aspx for more details.

Dustin Davis
  • 14,482
  • 13
  • 63
  • 119
  • 1
    From this blog post, it looks like the exception is automatically rethrown. If you set the FlowBeheavor to Continue, the exception is not rethrown. I think that is the opposite of what's said in this answer. – Chris Jan 31 '13 at 00:35
0

The attribute used in divide() method should be ExceptionAspectHandler (you've created), not OnExceptionAspect.

Silvio Delgado
  • 6,798
  • 3
  • 18
  • 22