javascript - 捕获错误发生后停止

标签 javascript node.js express error-handling throw

最近,我读到一种好的做法,即在出现错误时停止停止编写错误的结果,然后编写throw error。我不确定它是否适用于使用node.js和express构建的Web应用程序的服务器。我应该使用.catch处理所有throw error.吗?throw error会导致我的应用崩溃吗?

最佳答案

catch块是您处理错误的地方。换句话说,在这里您可能会触发函数的替代结果。例:

try {
  const result = await fetchSomeData();
  if(result !== null) {  
    return result;
  }else{
    throw "nothing was fetched"
  }
} catch(error) {
  console.log(error);
  const result = { error: "a custom error message", function: customCallBack };
  return result;
}

关于javascript - 捕获错误发生后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62732421/

相关文章:

javascript - PHP、Javascript 获取mysql数据并在javascript中进行计算

c# - ASP 从 C# 调用 javaScript

node.js - 使用路由 V4 在快速验证器上未定义检查

javascript - 来自组件的 Angular 2 getBoundingClientRect

javascript - express.js 和 angular.js 的区别在哪里?

mysql - 避免在 Node js 中的 SQL LIKE 查询中注入(inject)

node.js - 在 node.js Express 中获取当前请求的主机名

node.js - 是否有任何查询可以通过检查 mongoDb 中嵌套嵌入文档的字段来查找文档

node.js 和 express : use more than one views directory

javascript - jQuery:延迟变量 play() 函数?