博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
node.js 错误_Node.js中的错误处理
阅读量:2503 次
发布时间:2019-05-11

本文共 3438 字,大约阅读时间需要 11 分钟。

node.js 错误

Errors in Node.js are handled through exceptions.

Node.js中的错误通过异常处理。

创建例外 (Creating exceptions)

An exception is created using the throw keyword:

使用throw关键字创建一个异常:

throw value

As soon as JavaScript executes this line, the normal program flow is halted and the control is held back to the nearest exception handler.

JavaScript一旦执行此行,就会停止常规程序流,并将控件保留到最近的异常处理程序

Usually in client-side code value can be any JavaScript value including a string, a number or an object.

通常,在客户端代码中, value可以是任何JavaScript值,包括字符串,数字或对象。

In Node.js, we don’t throw strings, we just throw Error objects.

在Node.js中,我们不抛出字符串,而仅抛出Error对象。

错误对象 (Error objects)

An error object is an object that is either an instance of the Error object, or extends the Error class, provided in the :

错误对象是错误对象的实例,或者是提供的扩展错误类的对象:

throw new Error('Ran out of coffee')

or

要么

class NotEnoughCoffeeError extends Error {  //...}throw new NotEnoughCoffeeError

处理异常 (Handling exceptions)

An exception handler is a try/catch statement.

异常处理程序是try / catch语句。

Any exception raised in the lines of code included in the try block is handled in the corresponding catch block:

try块中包含的代码行中引发的任何异常都在相应的catch块中处理:

try {  //lines of code} catch (e) {}

e in this example is the exception value.

在此示例中, e是异常值。

You can add multiple handlers, that can catch different kinds of errors.

您可以添加多个处理程序,它们可以捕获各种错误。

捕获未捕获的异常 (Catching uncaught exceptions)

If an uncaught exception gets thrown during the execution of your program, your program will crash.

如果在程序执行过程中引发了未捕获的异常,则程序将崩溃。

To solve this, you listen for the uncaughtException event on the process object:

要解决此问题,请侦听process对象上的uncaughtException事件:

process.on('uncaughtException', (err) => {    console.error('There was an uncaught error', err)    process.exit(1) //mandatory (as per the Node docs)})

You don’t need to import the process core module for this, as it’s automatically injected.

您不需要为此导入process核心模块,因为它是自动注入的。

承诺的例外 (Exceptions with promises)

Using promises you can chain different operations, and handle errors at the end:

使用promise可以链接不同的操作,并在最后处理错误:

doSomething1()  .then(doSomething2())  .then(doSomething3())  .catch(err => console.error(err))

How do you know where the error occurred? You don’t really know, but you can handle errors in each of the functions you call (doSomethingX), and inside the error handler throw a new error, that’s going to call the outside catch handler:

您怎么知道错误发生在哪里? 您并不是很清楚,但是您可以处理所调用的每个函数( doSomethingX )中的错误,并且在错误处理程序内部将引发新错误,这将调用外部catch处理程序:

const doSomething1 = () => {  //...  try {    //...  } catch (err) {    //... handle it locally    throw new Error(err.message)  }  //...}

To be able to handle errors locally without handling them in the function we call, we can break the chain you can create a function in each then() and process the exception:

为了能够在本地处理错误而无需在我们调用的函数中处理错误,我们可以中断链,您可以在每个then()创建一个函数并处理异常:

doSomething1  .then((() => {    return doSomething2().catch(err => {      //handle error      throw err //break the chain!    })  })  .then((() => {    return doSomething2().catch(err => {      //handle error      throw err //break the chain!    })  })  .catch(err => console.error(err))

使用异步/等待处理错误 (Error handling with async/await)

Using async/await, you still need to catch errors, and you do it this way:

使用异步/等待,您仍然需要捕获错误,并且您可以通过以下方式进行操作:

async function someFunction() {  try {    await someOtherFunction()  }  catch (err) {    console.error(err.message)  }}

翻译自:

node.js 错误

转载地址:http://ktqgb.baihongyu.com/

你可能感兴趣的文章
表达式求值-201308081712.txt
查看>>
centos中安装tomcat6
查看>>
从Vue.js窥探前端行业
查看>>
学习进度
查看>>
poj3368 RMQ
查看>>
“此人不存在”
查看>>
github.com加速节点
查看>>
解密zend-PHP凤凰源码程序
查看>>
python3 序列分片记录
查看>>
Atitit.git的存储结构and 追踪
查看>>
atitit 读书与获取知识资料的attilax的总结.docx
查看>>
B站 React教程笔记day2(3)React-Redux
查看>>
找了一个api管理工具
查看>>
C++——string类和标准模板库
查看>>
zt C++ list 类学习笔记
查看>>
git常用命令
查看>>
探讨和比较Java和_NET的序列化_Serialization_框架
查看>>
1、jQuery概述
查看>>
数组比较大小的几种方法及math是方法
查看>>
FTP站点建立 普通电脑版&&服务器版
查看>>