How can I throw an exception using a variable within the exception name during the throw new
statement?
I have a list of exceptions that I'm trying to throw depending on whether or not one variable is zero in length. The name of the exception, for example, contains the variable name.
Works
if (X.length === 0)
{
throw new NoXException();
}
if (Y.length === 0)
{
throw new NoYException();
}
if (Z.length === 0)
{
throw new NoZException();
}
Doesn't work
var X = 'Oh';
var Y = 'Hai';
var Z = '';
// Contains "NoZException"
var ex = "No" + ((X.length != 0) ? ((Y.length != 0) ? 'Z' : 'Y') : 'X') + "Exception";
// Throwing ex gives an error
throw new ex();