2

Recently I came across the problem of creating an exception with a given message from within a generic method. For instance, the following code works as expected:

public static void Throw<T>() where T : Exception, new()
{
    throw new T();
}

...

public static void Main()
{
    Throw<ArgumentOutOfRangeException>(); // Throws desired exception but with a generic message.
}

However, I would like to be able to write

public static void Throw<T>(string message) where T : Exception, new()
{
    T newException = new T();

    newException.Message = message; // Not allowed. 'Message' is read-only.

    throw newException;
}

...

public static void Main()
{
    Throw<ArgumentOutOfRangeException>("You must specify a non-negative integer."); // Throws desired exception.
}

Is there any way of achieving this without the use of reflection either to change the value of the Message property or dinamically activate an instance of the type with the desired parameters?

DotNetStudent
  • 889
  • 9
  • 24

2 Answers2

8

You can use Activator.CreateInstance(typeof(T), "MyException description") to enable a custom message.

There is no way to create the instance without the use of the reflection or the use of the activator.

See

http://msdn.microsoft.com/de-de/library/wcxyzt4d(v=vs.80).aspx

Community
  • 1
  • 1
Felix K.
  • 6,201
  • 2
  • 38
  • 71
3

The new constraint is only valid for default (parameterless) constructors, try this instead:

public static void Throw<T>(string message) where T : Exception
{
    System.Reflection.ConstructorInfo constructor = typeof(T).GetConstructor(new Type[] { typeof(string) });
    T newException = (T)constructor.Invoke(new object[] { message });

    throw newException;
}

(note that you don't need the new constraint in this case)

vc 74
  • 37,131
  • 7
  • 73
  • 89