views:

292

answers:

3

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?

A: 

You should be able to do:

function ErrorTest(name)
{
    try
    {
        if (name.length < 5)
        {
            throw new ArgumentException();
        }else
        {
            throw new InvalidOperationException();
        }
    }
    catch (ArgumentException e)
    {
        return "Argument Exception: " + e.message;
    }
    catch (InvalidOperationException e)
    {
        return "Operation Exception: " + e.message;
    }
    catch (e)
    {
        return "Unknown Exception: " + e.message;
    }
}

and do separate error handling for each exception type that way.

Andy Mikula
Thanks, but the sample is JScript code, so the catch statement doesn't allow an exception type. All I seem to be allowed is a variable name.
Don Kirkby
Good point. You can throw new Error(number,"MessageString") and test the number that way. I think you might be out of luck trying to manipulate CLR exceptions from JScript.
Andy Mikula
A: 

You can get the original exception using the ErrorObject.ToException method, and then use instanceof to decide how to handle it.

This works, but I haven't decided whether to use it. I think it's abusing the framework, because the documentation says:

This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.

Also, you'll have to add a reference to the Microsoft.JScript assembly and import the Microsoft.JScript namespace.

Here's the new code:

function ErrorTest(name)
{
    try
    {
        if (name.length < 5)
        {
            throw new ArgumentException();
        }else
        {
            throw new InvalidOperationException();
        }
    }catch (e)
    {
        var ex = ErrorObject.ToException(e);
        if (ex instanceof ArgumentException)
        {
            return "bad argument";
        }
        return "something bad";
    }
}
Don Kirkby
I'm leaving this here for reference, but the accepted answer is obviously much better.
Don Kirkby
+3  A: 

Use JScript .NET's type annotation support, like so:

  try { }
  catch (e : ArgumentException) {
    // ...
  }
Christopher Done
Sweet! Where were you four months ago? I guess I should have kept hunting through the JScript documentation.
Don Kirkby
I thought the same thing! Where as I four months ago!? Indeed, I have read pretty much all of the JScript docs and been the better for it.
Christopher Done