我正在编写一个向服务器发出 Ajax 请求的代码。我在请求之外使用 try/catch 进行错误处理,如下所示。search 方法正在向服务器发送请求,如果它可以读取结果,则调用回调函数。
我在问这个:由于 ajax 请求是异步的,如果回调内部(而不是搜索方法内部)有错误,它会落入外部 catch 块还是我需要在回调函数中进行另一个 try/catch?
try {
service.search(function () {
var entries = service.getEntries();
$.each(entries, function (index, content) {
var cat = {
// those may throw exceptions, too
name: content.get("category"),
path: content.getPath()
};
categories.push(cat); // defined somewhere else.
});
if (categories.length < totalResults) {
// A method call may throw exception
} else if (typeof loadCallback === 'function') {
// Done.
}
});
} catch (e) {
}