Asking a Generic Method to Throw Specific Exception Type on FAIL

Right, I recognize I am entirely mosting likely to look a bonehead with this one, yet my mind is simply not starting to tailor today.

I intend to have a method where I can claim "if it spoils, return with this sort of Exception", right?

As an example, something like ( and also this does not function ):

    static ExType TestException<ExType>(string message) where ExType:Exception
    {
        Exception ex1 = new Exception();
        ExType ex = new Exception(message);
        return ex;
    }

Now whats complex me is that we KNOW that the common type is mosting likely to be of an Exception type as a result of the where condition. Nonetheless, the code falls short due to the fact that we can not unconditionally cast Exception to ExType . We can not clearly transform it either, such as:

    static ExType TestException<ExType>(string message) where ExType:Exception
    {
        Exception ex1 = new Exception();
        ExType ex = (ExType)(new Exception(message));
        return ex;
    }

As that falls short also. So is this example feasible? I have a solid sensation its mosting likely to be actual straightforward, yet I am having a challenging day with the old noggin, so reduce me some slack:P


Update

Thanks for the feedbacks individuals, resembles it had not been me being a full bonehead! ;)

OK, so Vegard and also Sam obtained me on the factor where I can instantiate the proper type, yet after that clearly obtained stuck due to the fact that the message param reads - just adhering to instantiation.

Matt struck the nail exactly on the head with his feedback, I have actually examined this and also all jobs penalty. Below is the instance code:

    static ExType TestException<ExType>(string message) where ExType:Exception, new ()
    {
        ExType ex = (ExType)Activator.CreateInstance(typeof(ExType), message);
        return ex;
    }

Sweet!

Many thanks individuals!

12
2022-06-07 15:16:45
Source Share
Answers: 4

The only concern with the remedy is that it is feasible to create a subdivision of Exception which does not implement an erector with a solitary string parameter, so the MethodMissingException could be tossed.

static void TestException<E>(string message) where E : Exception, new()
{
    try 
    {
      return Activator.CreateInstance(typeof(E), message) as E;
    } 
    catch(MissingMethodException ex) 
    {
      return new E();
    }
}
10
2022-06-07 18:08:30
Source

Have you attempted, rather:

static T TestException<Exception>(string message)
{}

due to the fact that I sense that placing in the common restraint is not essential as all throwable exemptions have to acquire from System.Exception anyhow.

Bear in mind that generics do approve acquired kinds.

1
2022-06-07 15:43:26
Source

I assume viewing as all exemptions need to have a parameterless erector, and also have the Message building, so the adhering to need to function:

static ExType TestException<ExType>(string message) where ExType:Exception
{
    ExType ex = new ExType();
    ex.Message = message;
    return ex;
}

Edit: OK, Message reads just, so you'll need to wish the class applies the Exception (string) erector rather.

static ExType TestException<ExType>(string message) where ExType:Exception
{
    return new ExType(message);
}
0
2022-06-07 15:43:09
Source

You can virtually do it similar to this:

static void TestException<E>(string message) where E : Exception, new()
{
    var e = new E();
    e.Message = message;
    throw e;
}

However, that does not compile due to the fact that Exception.Message reads just. It can just be appointed by passing it to the erector, and also there is no other way to constrict a common type with something apart from a default erector.

I assume you would certainly need to make use of representation (Activator.CreateInstance) to "new up" the personalized exception type with the message parameter, similar to this:

static void TestException<E>(string message) where E : Exception
{
    throw Activator.CreateInstance(typeof(E), message) as E;
}

Edit Oops simply know you are intending to return the exception, not toss it. The very same concept uses, so I'll leave my solution as - is with the toss declarations.

23
2022-06-07 15:42:48
Source