在 Chrome 的 JavaScript 控制台中,如果我运行这个:
var that = new XMLHttpRequest();
that.open('GET', 'http://this_is_a_bad_url.com', false);
that.send();
我收到一个故意预期的错误:
NetworkError: A network error occurred.
我想抓住这个,所以我使用:
var that = new XMLHttpRequest();
that.open('GET', 'http://this_is_a_bad_url.com', false);
try {
that.send();
} catch(exception) {
if(exception instanceof NetworkError) {
console.log('There was a network error.');
}
}
但是,我收到有关未定义 NetworkError 的错误:
ReferenceError: NetworkError is not defined
如何捕获 NetworkError?