简体   繁体   English

Javascript Try / Catch

[英]Javascript Try/Catch

I've got a function that runs a user generated Regex. 我有一个运行用户生成的正则表达式的函数。 However, if the user enters a regex that won't run then it stops and falls over. 但是,如果用户输入了不会运行的正则表达式,则它会停止并翻倒。 I've tried wrapping the line in a Try/Catch block but alas nothing happens. 我已经尝试在Try / Catch块中包装该行但是没有任何反应。

If it helps, I'm running jQuery but the code below does not have it as I'm guessing that it's a little more fundamental than that. 如果它有帮助,我正在运行jQuery但是下面的代码没有它,因为我猜它比那更基础。

Edit: Yes, I know that I am not escaping the "[", that's intentional and the point of the question. 编辑:是的,我知道我没有逃避“[”,这是故意和问题的重点。 I'm accepting user input and I want to find a way to catch this sort of problem without the application falling flat on it's face. 我正在接受用户输入,我想找到一种方法来捕捉这种问题,而不会让应用程序掉到脸上。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <title>Regex</title>

    <script type="text/javascript" charset="utf-8">
        var grep = new RegExp('gr[');

        try
        {
            var results = grep.exec('bob went to town');
        }
        catch (e)
        {
            //Do nothing?
        }

        alert('If you can see this then the script kept going');
    </script>
</head>
<body>

</body>
</html>

Try this the new RegExp is throwing the exception 试试这个新的RegExp抛出异常

Regex 正则表达式

    <script type="text/javascript" charset="utf-8">
            var grep;

            try {
                    grep = new RegExp("gr[");
            }
            catch(e) {
                    alert(e);

            }
            try
            {
                    var results = grep.exec('bob went to town');
            }
            catch (e)
            {
                    //Do nothing?
            }

            alert('If you can see this then the script kept going');
    </script>

The problem is with this line: 问题出在这一行:

var grep = new RegExp('gr[');

'[' is a special character so it needs to be escaped. '['是一个特殊字符,因此需要进行转义。 Also this line is not wrapped in try...catch, so you still get the error. 此行也不包含在try ... catch中,因此您仍然会收到错误。

Edit : You could also add an 编辑 :你也可以添加一个

alert(e.message);

in the catch clause to see the error message. 在catch子句中查看错误消息。 It's useful for all kind of errors in javascript. 它对javascript中的所有错误都很有用。

Edit 2 : OK, I needed to read more carefully the question, but the answer is still there. 编辑2 :好的,我需要仔细阅读这个问题,但答案仍然存在。 In the example code the offending line is not wrapped in the try...catch block. 在示例代码中,违规行未包含在try ... catch块中。 I put it there and didn't get errors in Opera 9.5, FF3 and IE7. 我把它放在那里,并没有在Opera 9.5,FF3​​和IE7中出错。

var grep, results;

try {
    grep = new RegExp("gr[");
    results = grep.exec('bob went to town');
}
catch(e) {
    alert(e);
}
alert('If you can see this then the script kept going');

putting the RegExp initialization inside the try/catch will work (just tested in FireFox) 将RegExp初始化放在try / catch中将起作用(仅在FireFox中测试)


var grep, results;

try
{
    grep = new RegExp("gr["); // your user input here
}
catch(e)
{
    alert("The RegExpr is invalid");
}

// do your stuff with grep and results

Escaping here is not the solution. 逃避这里不是解决方案。 Since the purpose of this snippet is to actually test a user-generated RegExpr, you will want to catch [ as an unclosed RegExpr container. 由于此代码段的目的是实际测试用户生成的RegExpr,因此您需要捕获[作为未关闭的RegExpr容器。

your RegExp doesn't close the [ 你的RegExp没有关闭[

In my FireFox, it never returns from the constructor -- looks like a bug in the implementation of RegExp, but if you provide a valid expression, it works 在我的FireFox中,它永远不会从构造函数返回 - 看起来像是RegExp实现中的一个错误,但是如果你提供一个有效的表达式,它就有效

One option is to validate the user-generated expressions. 一种选择是验证用户生成的表达式。 That is; 那是; escape characters that you know will stall your script. 你知道的转义字符会使你的脚本失效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM