1. 程式人生 > 實用技巧 >nodejs.cn-Node.js-入門教程:Node.js 中的錯誤處理

nodejs.cn-Node.js-入門教程:Node.js 中的錯誤處理

ylbtech-nodejs.cn-Node.js-入門教程:Node.js 中的錯誤處理

1.返回頂部
1、

Node.js 中的錯誤處理

目錄

Errors in Node.js are handled through exceptions.

Creating exceptions

An exception is created using thethrowkeyword:

throw value

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

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

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

Error objects

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

throw new Error('Ran out of coffee')

or

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

Handling exceptions

An exception handler is atry/catchstatement.

Any exception raised in the lines of code included in thetryblock is handled in the correspondingcatchblock:

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

ein this example is the exception value.

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 theuncaughtExceptionevent on theprocessobject:

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

You don't need to import theprocesscore module for this, as it's automatically injected.

Exceptions with promises

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

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 outsidecatchhandler:

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 eachthen()and process the exception:

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)
  }
}
2、
2.返回頂部
3.返回頂部
4.返回頂部
5.返回頂部
1、 http://nodejs.cn/learn/error-handling-in-nodejs 2、
6.返回頂部
作者:ylbtech
出處:http://ylbtech.cnblogs.com/
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。