Catch语句没有抓住抛出的错误 [英] Catch statement does not catch thrown error

查看:141
本文介绍了Catch语句没有抓住抛出的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,此代码给我一个未捕获的异常错误。看来catch块没有捕捉错误。尝试捕获框架的范围是这样一种方式,我不能在嵌套函数中抛出一个错误,然后期望它被链接的较高的catch语句捕获?在我正在工作的应用程序中的一些敏感数据已被删除,但它预计leadInfo [0/1]将是一个32字符的字母数字字符串,我从URL参数中提取。



这里的基本问题是我的AJAX调用从API返回错误,该错误在应用程序中未正确处理。因此需要throw语句。 AJAX调用完成,并返回不包含电子邮件地址作为属性的JSON对象,因此我需要以更改页面的方式来处理该对象。


$ b $ ($($($)
$ {
url =http://api.com/api/v1/lead /+ leadInfo [1]

jQuery.ajax({
type:'GET',
contentType:'application / json',
url:url,
dataType:'jsonp',
success:function(result){

result = jQuery.parseJSON(result);

if(!result .data.email){
throw('New exception');
}
console.log(result);
jQuery('。email')。 data.email);
}
});


jQuery( .surveryButton')。click(function(){
window.location.replace(http://+ pgInventory.host + pgInventory.path + leadInfo [0] +& curleadId =+ leadInfo [ 1] +& curViewedPages = 0);
});
}
catch(err){
jQuery('。email')。html('your e-mail address');
jQuery('#arrowContent')。remove();
}
});


解决方案

catch 块失败是因为ajax请求是异步的。 try catch块将在Ajax调用之前执行,然后发送请求本身,但返回结果时会抛出错误。在AT A LATER POINT IN TIME。



当执行 try catch 块,没有错误。当抛出错误时,没有 try catch 。如果您需要尝试捕获为ajax请求,请始终在成功中将ajax try catch blocks 回调,不要在它之外。



这是你应该如何做的:

  success:function(result){
try {
result = jQuery.parseJSON(result);

if(!result.data.email){
throw('New exception');
}
console.log(result);
jQuery('。email')。html(result.data.email);
} catch(exception){
console.error(bla);
};
}


For some reason this code gives me an uncaught exception error. It seems the catch block is not catching the error. Are try catch blocks scoped in such a way that I cannot throw an error in a nested function, and then expect it to be caught by a catch statement scoped higher up the chain? Some of the sensitive data with in the application that i'm working in has been removed, but it expected that leadInfo[ 0 / 1] would be a 32 character alpha numeric string that I pull from URL parameters.

The underlying issue here is with my AJAX call returning an error from the API and that error not being handled properly within the application. Hence the need for the throw statement. The AJAX call completes fine, and returns a JSON object that does not contain the email address as a property, so I need to handle that in a way that changes the page to reflect that.

    jQuery(document).ready(function(){
        try {
            url = "http://api.com/api/v1/lead/" + leadInfo[1]

            jQuery.ajax({
                type: 'GET',
                contentType: 'application/json',
                url: url,
                dataType : 'jsonp',
                success: function (result) {

                    result = jQuery.parseJSON(result);

                    if(!result.data.email){
                        throw ('New exception');
                    }
                    console.log(result);
                    jQuery('.email').html(result.data.email);
                }
            });


            jQuery('.surveryButton').click(function(){
                window.location.replace("http://" + pgInventory.host + pgInventory.path + leadInfo[0] + "&curLeadId=" + leadInfo[1] + "&curViewedPages=0");
            });
        }
        catch(err) {
            jQuery('.email').html('your e-mail address');
            jQuery('#arrowContent').remove();
        }
});

解决方案

The reason why your try catch block is failing is because an ajax request is asynchronous. The try catch block will execute before the Ajax call and send the request itself, but the error is thrown when the result is returned, AT A LATER POINT IN TIME.

When the try catch block is executed, there is no error. When the error is thrown, there is no try catch. If you need try catch for ajax requests, always put ajax try catch blocks inside the success callback, NEVER outside of it.

Here's how you should do it:

success: function (result) {
    try {
      result = jQuery.parseJSON(result);

      if (!result.data.email) {
         throw ('New exception');
      }
      console.log(result);
      jQuery('.email').html(result.data.email);
    } catch (exception) {
      console.error("bla");
    };
}

这篇关于Catch语句没有抓住抛出的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆