【发布时间】:2018-07-21 00:15:50
【问题描述】:
为什么下面的代码没有捕捉到抛出的异常?
$http.get(null) // <- this throws a fit
.catch(function (e) {
console.log(e); // <- this is not being triggered
});
错误:[$http:badreq] Http 请求配置 url 必须是字符串或 $sce 可信对象。收到:空 https://errors.angularjs.org/1.7.2/$http/badreq?p0=null
【问题讨论】:
-
我认为您将 try/catch 与您承诺的
catch处理程序混淆了。第一个是抛出一个合适的,因为 null 不是可接受的输入参数。您需要将整个内容包装在try {...} catch (err) {...} -
$http.get方法在创建并返回一个承诺之前 抛出错误。
标签: javascript angularjs promise