📜  Node中的异常处理

📅  最后修改于: 2022-05-13 01:56:57.192000             🧑  作者: Mango

Node中的异常处理

异常处理是指在应用程序运行时处理代码中发生的异常的机制。 Node.js 支持多种传播和处理错误的机制。
这是可用于 Node.js 中异常处理的不同方法:

  • 同步代码中的异常处理:如果同步代码发生错误,则返回错误。
    例子:
    // Write Javascript code here
    // Define divider as a syncrhonous function
    var divideSync = function(x, y) {
        // if error condition?
        if ( y === 0 ) {
            // "throw" the error safely by returning it
            return new Error("Can't divide by zero")
        }
        else {
            // no error occurred, continue on
            return x/y
        }
    }
      
    // Divide 9/3
    var result = divideSync(9, 3)
    // did an error occur?
    if ( result instanceof Error ) {
        // handle the error safely
        console.log("9/3=err", result)
    }
    else {
        // no error occurred, continue on
        console.log("9/3="+result)
    }
      
    // Divide 9/0
    result = divideSync(9, 0)
    // did an error occur?
    if ( result instanceof Error ) {
        // handle the error safely
        console.log("9/0=err", result)
    }
    else {
        // no error occurred, continue on
        console.log("9/0="+result)
    }
    

    输出:

  • 基于回调(异步)代码中的异常处理:在基于回调的代码中,回调的参数之一是 err。如果发生错误,则 err 为错误,如果未发生错误,则 err 为空。 err 参数后面可以跟任意数量的其他参数。
    例子:
    // Write Javascript code here
    var divide = function(x, y, next) {
        // if error condition?
        if ( y === 0 ) {
            // "throw" the error safely by calling the completion callback
            // with the first argument being the error
            next(new Error("Can't divide by zero"))
        }
        else {
            // no error occurred, continue on
            next(null, x/y)
        }
    }
      
    divide(9, 3, function(err, result){
        // did an error occur?
        if ( err ) {
            // handle the error safely
            console.log("9/3=err", err)
        }
        else {
            // no error occurred, continue on
            console.log("9/3="+result)
        }
    })
      
    divide(9, 0, function(err, result){
        // did an error occur?
        if ( err ) {
            // handle the error safely
            console.log("9/0=err", err)
        }
        else {
            // no error occurred, continue on
            console.log("9/0="+result)
        }
    })
    

    输出:

  • 事件代码中的异常处理:在事件代码中,错误可能发生在任何地方。因此,与其抛出错误,不如触发错误事件。
    例子:
    // Write Javascript code here
    // Definite our Divider Event Emitter
    var events = require("events")
    var Divider = function(){
        events.EventEmitter.call(this)
    }
    require('util').inherits(Divider, events.EventEmitter)
      
    // Add the divide function
    Divider.prototype.divide = function(x, y){
        // if error condition?
        if ( y === 0 ) {
            // "throw" the error safely by emitting it
            var err = new Error("Can't divide by zero")
            this.emit("error", err)
        }
        else {
            // no error occurred, continue on
            this.emit("divided", x, y, x/y)
        }
      
        // Chain
        return this;
    }
      
    // Create our divider and listen for errors
    var divider = new Divider()
    divider.on('error', function(err){
        // handle the error safely
        console.log(err)
    })
    divider.on('divided', function(x, y, result){
        console.log(x+"/"+y+"="+result)
    })
      
    // Divide
    divider.divide(9, 3).divide(9, 0)
    

    输出: