【问题标题】:Catch specific exception types in NodeJS [duplicate]在 NodeJS 中捕获特定的异常类型 [重复]
【发布时间】:2022-01-06 08:49:02
【问题描述】:

在 C# 和 Python 中,我可以将代码包装在 try-except 子句中,其中包含针对不同异常类的多个 except 部分。

try:
 # Do something
except ExceptionType1:
 # return A
except ExceptionType2:
 # return B

我可以在 NodeJS 中做类似的事情吗?

【问题讨论】:

  • @GOTO0 - 是的。谢谢!

标签: node.js try-catch


【解决方案1】:
yeah why not you can use try catch in node also. let look at axample below

const GetUser = async(req , res)=>{

try{
let id = req.body.id;
let users = await Users.findAll({where:{id:id}});
if(!users) throw "no user found"
res.send('user found');
}catch(error){
res.send(error);
}

【讨论】:

  • 这不是我想要的。我想要几个 catch 子句,每个子句专用于不同的异常类型
猜你喜欢
  • 2011-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-08
  • 1970-01-01
  • 1970-01-01
  • 2011-07-06
相关资源
最近更新 更多