node.js Nodejs 抛出异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34017767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:35:59  来源:igfitidea点击:

Nodejs throw exception

node.js

提问by Muhamet Aljobairi

I'm working on Nodejs service-side application, my case is that i want to return the throw an exception to the caller(who did call the function), i did two cases, one outside the callback, and the another inside the callback, also the parent has try catch blocks.

我正在研究 Nodejs 服务端应用程序,我的情况是我想将抛出异常返回给调用者(谁调用了函数),我做了两种情况,一种在回调之外,另一种在回调内,父级也有 try catch 块。

concept : throw(Business function)-->throw (Business function) -->try & catch

概念:throw(业务函数)-->throw(业务函数)-->try & catch

Outside the callback is working correctly. Inside the callback didn't return the exception to the parent.

外部回调工作正常。在回调内部没有将异常返回给父级。

I want to to this scenario,because I'm looking to throw an exception to the parent and stop completion the functions, and this's exist in Java, C++, C and .NET.

我想要这种情况,因为我希望向父级抛出异常并停止完成函数,这在 Java、C++、C 和 .NET 中存在。

So why this scenario is not working with me ?!

那么为什么这个场景对我不起作用?!

My example here with 2 different cases :

我这里的例子有两种不同的情况:

FactoryController.prototype.create = function (callback) {
    //The throw is working, and the exception is returned.
    throw new Error('An error occurred'); //outside callback 
    try {
        this.check(function (check_result) {
            callback(check_result);
        });
    } catch (ex) {
        throw new Error(ex.toString());
    }
}

FactoryController.prototype.create = function (callback) {
    try {
        this.check(function (check_result) {
            //The throw is not working on this case to return the exception to the caller(parent)
            throw new Error('An error occurred'); //inside callback 
        });
    } catch (ex) {
        throw new Error(ex.toString());
    }
}

采纳答案by Tapirboy

Exceptions occurs because you are throwing the errors. If you rather want to return the error to the caller, you need to provide it in the callback. Add the error as parameter to the callback.

发生异常是因为您抛出错误。如果您希望将错误返回给调用者,则需要在回调中提供它。将错误作为参数添加到回调中。

Usually the callback pattern is callback(error, result);

通常回调模式是 callback(error, result);

callback(new Error(ex.toString())); // ignore result param

Error handling in Nodejs

Nodejs 中的错误处理

回答by Chamath Nanayakkara

would the below code satisfy your requirement

下面的代码是否满足您的要求

'use strict';
var  inherits =require('util').inherits;
//Parent
function factoryController(n){
     this.name=n;
 }
 //parent prototype function
 factoryController.prototype.create = function (type) {
     var retVal="";
     try {
     throw new Error('An error occurred'); 
      retVal=this.name+' is a '+ type;
     }
     catch(e){
         retVal=e.toString();
     }
     return retVal;

}
 function subsidaryController(name) {
       // Call parent constructor 
        factoryController.call(this, name);

    }
inherits(subsidaryController,factoryController);
// Additional member functions 
 subsidaryController.prototype.fn = function (locations) {
     var retVal="";
     try {
          throw new Error('Another error occurred'); 
          retVal='Having branches in' + locations;
     }
     catch(e){
         retVal=e.toString();
     }
     return retVal;
 }   
 let fc = new subsidaryController('ABC Factory');
console.log(fc.create('Manufacturer' ));
console.log(fc.fn('Europe and Asia'));