首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JINT -如何在JINT中捕获CatchClrExceptions中的异常?

JINT -如何在JINT中捕获CatchClrExceptions中的异常?
EN

Stack Overflow用户
提问于 2018-01-22 04:46:56
回答 1查看 1.5K关注 0票数 0

我想捕捉从javascript抛出的clr异常。下面是我试过的东西。

代码语言:javascript
复制
var registerScript = new Engine(c => c.AllowClr(typeof(Manager).Assembly ).CatchClrExceptions(ExceptionHandler(new Exception("Exception")) )).Execute("javascriptCode").GetValue("JavascriptFunction");

public static Predicate<Exception> ExceptionHandler(Exception ex)
{
      throw new Exception(ex.Message);
}

但我想我喜欢这个,

代码语言:javascript
复制
 var registerScript = new Engine(c => c.AllowClr(typeof(Manager).Assembly ).CatchClrExceptions(e=>ExceptionHandler(new Exception(e.Message)) )).Execute("javascriptCode").GetValue("JavascriptFunction");

也就是说,我想从javascript捕获异常并获取异常消息。

请帮我一下。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-22 05:06:29

CatchClrExceptions允许您捕获来自程序集的异常,并直接检查Jint引擎应该将异常传递给JS代码,或者将异常作为.NET异常抛出。

代码语言:javascript
复制
namespace ConsoleApp5
{
    public class Program
    {
        public static void Helper(string msg)
        {
            throw new Exception(msg);
        }
        static void Main(string[] args)
        {
            var registerScript = new Engine(c => c
                .AllowClr(typeof(Program).Assembly)
                // Allow exceptions from this assembly to surface as JS exceptions only if the message is foo
                .CatchClrExceptions(ex => ex.Message == "foo")
            )
            .Execute(@"function throwException(){ 
                    try { 
                        var ConsoleApp5 = importNamespace('ConsoleApp5');
                        ConsoleApp5.Program.Helper('foo'); 
                        // ConsoleApp5.Program.Helper('goo'); // This will fail when calling execute becase the predicate returns false 
                        return ''; 
                    }  
                    catch(e) { 
                        return e; 
                    } 
            };
            var f = throwException();")
            .GetValue("f");
        }
    }
}

传递给CatchClrExceptionsCatchClrExceptions应该返回true/false。它将接收引发的CLR异常。

对于Jint运行时处理的异常,似乎无法得到通知。要捕获解析器异常(即无效的JS代码),可以用常规的Execute包围try..catch(ParserException ex) { .. },这将捕获任何解析异常。对于运行时异常,您还可以捕获JavaScriptException,它将在未处理的异常执行时抛出。

代码语言:javascript
复制
var engine = new Engine(c => c
    .DebugMode()
    .AllowClr(typeof(Program).Assembly)
);

engine.Step += Engine_Step;

try
{
    var r = engine
    .Execute(@"function throwException(){ 
        // undefined.test(); // This will cause a runtime exception
        // fun ction test () { } // This will cause a parser exception
        try { 
            throw 'Handled exception'; // No notification on this exception it is handled in JS
            return ''; 
        }  
        catch(e) { 
            return e; 
        } 
};
var f = throwException();")
    .GetValue("f");
}
catch (ParserException pEx)
{
    Console.WriteLine("Parser Exception " + pEx.Message);
}
catch (JavaScriptException rEx)
{
    Console.WriteLine("Runtime Exception " + rEx.Message);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48374822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档