I’m using JScript.NET to write scripts in a C# WinForms application I wrote. It works really well, but I just tried putting some exception handling in a script, and I can’t figure out how to tell what type of exception was thrown from my C# code.
Here’s some example JScript code that throws two different types of CLR exception, and then tries to catch them and tell them apart.
function ErrorTest(name)
{
try
{
if (name.length < 5)
{
throw new ArgumentException();
}else
{
throw new InvalidOperationException();
}
}catch (e)
{
return e.name + " " + (e.number & 0xFFFF) + ": " + e.message;
}
}
If I call ErrorTest("foo"), then I get back "Error 5022: Value does not fall within the expected range.", and if I call ErrorTest("foobar"), then I get back "Error 5022: Operation is not valid due to the current state of the object."
The name and number properties are identical, the only difference is the message. I don’t want to start writing error handling logic based on the error messages, so is there some way to get at the original exception instead of the JScript Error object?
Use JScript .NET’s type annotation support, like so: