Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: Getting JS exception

0
0
Hello!

This question is similar to your earlier one about catching .NET exceptions in JavaScript, and our answer is similar as well - consider catching the exception in JavaScript and allowing .NET code to inspect it. For example:
engine.Execute(@"
    function tryCatch(func) {
        try {
            return { isSuccess: true, returnValue: func() };
        }
        catch (exception) {
            return { isSuccess: false, exception: exception };
        }
    }
");
and then:
dynamic result = engine.Evaluate(@"tryCatch(function() {
    throw { foo: 123, bar: 456 };
})");

if (!result.isSuccess)
{
    Console.WriteLine(result.exception.foo);
    Console.WriteLine(result.exception.bar);
}
else
{
    Console.WriteLine(result.returnValue);
}
As you can see, the host and the script engine can work together!

Good luck!

Viewing all articles
Browse latest Browse all 2297