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

New Post: Handling CLR exceptions in JS

0
0
Hello!

ClearScript 5.4 does include a generalized version of tryCatch(). Here's an example:
engine.AddHostObject("host", new HostFunctions());
engine.AddHostType("Console", typeof(Console));
engine.AddHostType("WebClient", typeof(WebClient));
engine.Execute(@"
    var webClient = new WebClient();
    host.tryCatch(
        function () {
            // download web document
            Console.WriteLine(webClient.DownloadString('http://cnn.com'));
        },
        function (exception) {
            // dump exception
            Console.WriteLine('*** ERROR: ' + exception.GetBaseException().ToString());
            return true;
        },
        function () {
            // clean up
            Console.WriteLine('*** CLEANING UP ***');
            webClient.Dispose();
        }
    );
");
Please let us know if handling host exceptions this way isn't practical in your scenario. Unlike the older script engines, V8 could probably enable a more natural pattern, although ClearScript currently doesn't support that.

Thanks!

Viewing all articles
Browse latest Browse all 2297