errors with Javascript try catch
errors with Javascript try catch
我用Javascript编写了一个项目,并使用try/catch,如下所示:
try{
var params = {
't_id': msg.from.username,
0: config.botToken
};
request.post({
url: urls.get('favorites'),
json:true,
body: {
jsonrpc: '2.0',
method: 'favorites',
params: params,
id: 1
}
}, function(error, response, body) {
if(body.result == undefined)
throw "other";
});
}catch(err)
{
let message = messages.getMessages(err);
console.log(message);
}
启动程序后,显示此错误:
/var/www/html/marketer_bot/classes/Products.js:54抛出"其他";
例如:Products.js:54
表示throw "other";
我不知道出了什么问题!你能帮我吗?
您正在抛出一个异步执行的回调。这意味着代码块将在try-catch
语句运行完毕后执行。
当回调执行时,您需要处理回调中的错误:
request.post({
url: urls.get('favorites'),
json:true,
body: {
jsonrpc: '2.0',
method: 'favorites',
params: params,
id: 1
}
}, function(error, response, body) {
if(body.result == undefined) {
// handle error right here
}
});
相关文章:
- errors with Javascript try catch
- JSON.parse,已经在try/catch块中,仍然抛出语法错误
- 错误处理try&catch+回调
- 在try-catch块内的HTML文件中导入JavaScript文件
- 有没有一种方法可以将try-catch添加到Javascript中的每个函数中
- Javascript Try Catch for DIV
- Safari 在尝试使用 Javascript try/catch 访问父窗口对象时未捕获异常
- 与在异步回调链中使用 try catch 块相比,返回异常有什么优势
- 在 Javascript 中,即使从未抛出异常,使用 try-catch 块是否昂贵
- JavaScript 中的 try-catch:如何获取原始错误的堆栈跟踪或行号
- 使用 try catch(e) 进行浏览器功能检测
- 了解 JavaScript 中的异常处理:当更改 try/catch 块的位置时,获得不同的输出
- 在 JavaScript 中用“try catch”包装每个方法的性能问题
- execScript try/catch in IE 8 中的怪异之处
- Javascript代码仅在FireFox中的try-catch块中失败
- 在 AJAX 响应中查找嵌套属性,这是 try/catch 的情况
- 使用 window.onerror 和 try-catch 块处理错误之间的差异
- 在声明变量时使用 try catch
- Array.map+try/catch传递了引用赋值延迟
- 在PHP中使用Try-catch结构