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

Commented Unassigned: Throw specific script exception by host [43]

0
0
Hi,

Would it be possible to add a custom exception, so that when it is thrown by host it throws a script exception of the specified type and message.

For example, in host code:

```
throw new CustomScriptException("RangeError", "custom message");
```

will cause script to execute:

```
throw new RangeError('custom message');
```

Specifically, for the example above I need the exception to be instanceof RangeError.

Thanks again in advance,
Ron


Comments: That's an interesting idea, but we'd prefer not to introduce language-specific API elements. Some script engines (e.g., JScript) don't have exception throwing APIs, and some others (e.g., VBScript) don't support exceptions at all. Instead, we recommend that you use script code to throw script exceptions. To facilitate that, you could set up a function that passes host exceptions to script code: ``` C# engine.Script.tryCatch = new Func<dynamic, object>(func => { try { return new { returnValue = func() }; } catch (Exception exception) { return new { exception }; } }); ``` You could then use this function to implement the required exception handling behavior: ``` JavaScript var result = tryCatch(function () { // invoke host methods/properties }); if (result.exception) { // convert host exception and throw throw toJavaScriptException(result.exception); } // use result.returnValue ```

Viewing all articles
Browse latest Browse all 2297