I am trying to throw an exception based on an exception type parameter passed to the method.
Here is what I have, but I do not want to indicate each kind of exception:
public void ThrowException<T>(string message = "") where T : SystemException, new() { if (ConditionMet) { if(typeof(T) is NullReferenceException) throw new NullReferenceException(message); if (typeof(T) is FileNotFoundException) throw new FileNotFoundException(message); throw new SystemException(message); } }
Ideally, I want to do something like new T(message) , given that I have a basic SystemException type. I would think that this is possible.
source share