Search code examples
postsharp

Exceptions are simply not caught by PostSharp


Well, I don't get it. Trying to apply a PostSharp attribute for exception handling on a method, the aspect's OnException method is simply not reached by any means. This is very disappointing. For the sake of testing, let's say this is the aspect:

[PSerializable]
public class HandleExceptionsAttribute : OnExceptionAspect
{
    public override void OnException(MethodExecutionArgs args)
    {
        Trace.TraceError(args.Exception.Message);            
        args.FlowBehavior = FlowBehavior.Return;
    }
}

And it is applied on a method that only throws an exception:

[HandleExceptions]
public static void Test()
{
    throw new Exception("Test");
}

Well, nothing happens. The aspect's OnException method is never reached at all (via debugging or otherwise).

What am I missing?


Solution

  • Having the PostSharp NuGet package installed, the aspect code should be reached, but break-points within the aspect code might not be reached. You need to have PostSharp Tools for Visual Studio installed to make debugging of aspect code work. You can download it at https://www.postsharp.net/download.