TypeError - 无法继续捕获Catch异常

时间:2018-05-17 03:53:27

标签: javascript exception-handling

我无法让脚本在遇到异常时继续运行。无论何时停止在.split工作

我尝试了几个级别的catch异常并得到了相同的结果。

根据我的理解,脚本应该继续运行,但我得到了不同的结果。

如果我忽略了任何事情,请提供建议。

    function general(html){
    try {
        GM_xmlhttpRequest({
        method: "GET",
        url: ""+url,
        synchronous: true,
        onload: function(response) {
            var e = response.responseText.split('<source src="')[1].split('"')[0];
           }
        });
    } catch (exception) {
        //Ignore Exception & Continue
    }
}


Uncaught TypeError: Cannot read property 'split' of undefined
at Function.onload (script.html?id=8705b42e-a72f-4a45-a971-8ecdea05d096:545)
at <anonymous>:2:491
at eval (eval at exec_fn (X:1), <anonymous>:31:148)

1 个答案:

答案 0 :(得分:0)

我找到了一种方法,我基本上检查了字符串是否存在。

仅在字符串存在时才继续。肮脏的修复,但它的工作原理。

检测字符串的代码

(ES6)
var string = "foo",
    substring = "oo";
string.includes(substring);

我的固定代码

 var substring = "<source";
        var result = response.responseText.includes(substring);
        var e = null
        if(result === true){
            e = response.responseText.split('<source src="')[1].split('"')[0];
        }