Let’s suppose we want to throw if we try to assign null to something, what about this trick:
public static class ExceptionExtension
{
public static T Throw<T>(this Exception exc)
{
throw exc;
}
}
that we can use for example like this:
return possibleNull ?? new Exception("Unspecified something....").Throw<string>();
do you think it is a good/worst/useless practice ?
It makes no sense to me – not very readable. I would expect the second argument of the
??operator to be of the same type ofpossibleNull, not to throw an excpetion.I would much rather see: