Open sandboxFocusImprove this doc

LA0247: Exception Parameter of the Wrong Type

Severity Error
Reported by PostSharp
Message The [Exception] parameter "<Parameter>" of the advice "<Advice>" is of type "<ParameterType>". The parameter must be of type "System.Exception", or of type "object" when it is passed by value.

Cause

An [ExceptionAttribute] parameter of an OnMethodExceptionAdvice receives the exception that the method throws, from a variable of type Exception. The exception can be of any type, so the parameter cannot be of a derived type such as InvalidOperationException. A parameter passed by value can also be of type object. A parameter passed as out or ref is the variable itself, so it must be exactly of type Exception.

PostSharp reports LA0247 when the parameter <Parameter> of the advice <Advice> is of another type, <ParameterType>.

In the following example, the exception parameter is of type InvalidOperationException.

using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;

namespace Errors.LA0247_Error;

[PSerializable]
public class LogFailureAttribute : MethodLevelAspect
{
    [OnMethodExceptionAdvice]
    public static void OnException(
        [DeclarationName] string methodName,
        // The parameter cannot be of a type derived from Exception.
        [Exception] InvalidOperationException exception )
    {
        Console.WriteLine( $"{methodName} failed: {exception.Message}" );
    }
}

public class OrderService
{
    [LogFailure]
    public void PlaceOrder( string product ) => throw new InvalidOperationException( $"{product} is out of stock." );
}

public static class Program
{
    public static void Main()
    {
        try
        {
            new OrderService().PlaceOrder( "Keyboard" );
        }
        catch ( InvalidOperationException e )
        {
            Console.WriteLine( $"Caught: {e.Message}" );
        }
    }
}
error LA0247: LA0247_Error.cs(15,47): The [Exception] parameter "exception" of the advice "Errors.LA0247_Error.LogFailureAttribute.OnException(System.String, System.InvalidOperationException)" is of type "System.InvalidOperationException". The parameter must be of type "System.Exception", or of type "object" when it is passed by value.

Resolution

Declare the parameter as Exception, or as object when it is passed by value. To handle only some types of exceptions, test the type of the exception in the advice, for instance with the is operator.

In the following example, the exception parameter is of type Exception.

using System;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Serialization;

namespace Errors.LA0247_Fixed;

[PSerializable]
public class LogFailureAttribute : MethodLevelAspect
{
    [OnMethodExceptionAdvice]
    public static void OnException(
        [DeclarationName] string methodName,
        // The parameter is of type Exception, which receives any exception.
        [Exception] Exception exception )
    {
        Console.WriteLine( $"{methodName} failed: {exception.Message}" );
    }
}

public class OrderService
{
    [LogFailure]
    public void PlaceOrder( string product ) => throw new InvalidOperationException( $"{product} is out of stock." );
}

public static class Program
{
    public static void Main()
    {
        try
        {
            new OrderService().PlaceOrder( "Keyboard" );
        }
        catch ( InvalidOperationException e )
        {
            Console.WriteLine( $"Caught: {e.Message}" );
        }
    }
}
PlaceOrder failed: Keyboard is out of stock.
Caught: Keyboard is out of stock.

See Also

Reference

ExceptionAttribute
OnMethodExceptionAdvice

Other Resources

Advice Parameters