admin管理员组

文章数量:1426594

What is the cleanest way to both throw an error and return a value from a Javascript function?

Here's one proposed approach as a starting point:

(e, v) => {
   setTimeout(() => { throw(e) }, 0);
   return v; 
}

Here's a runnable snippet to demonstrate further:

var val = ((e, v) => {
   setTimeout(() => { throw(e) }, 0); // will throw in console
   return v; 
})('errMSG', 1)


alert(val); // 1 

What is the cleanest way to both throw an error and return a value from a Javascript function?

Here's one proposed approach as a starting point:

(e, v) => {
   setTimeout(() => { throw(e) }, 0);
   return v; 
}

Here's a runnable snippet to demonstrate further:

var val = ((e, v) => {
   setTimeout(() => { throw(e) }, 0); // will throw in console
   return v; 
})('errMSG', 1)


alert(val); // 1 

Share Improve this question edited Feb 14, 2018 at 3:21 Dexygen 12.6k13 gold badges86 silver badges151 bronze badges asked Feb 12, 2018 at 19:03 hally9khally9k 2,5932 gold badges29 silver badges51 bronze badges 7
  • I'd probably create a custom response class with an error code and response wrapped up. – nurdyguy Commented Feb 12, 2018 at 19:05
  • 1 The proposed approach is broken as the exception is thrown from within an async callback. The original function just returns v, it doesn't throw any exceptions. – Wiktor Zychla Commented Feb 12, 2018 at 19:08
  • 3 Why do you want to do this? – Patrick Roberts Commented Feb 12, 2018 at 19:49
  • @PatrickRoberts I am using RxJS epics in my Redux application, when an epic fails I want to both throw the error and return the source observable so that the epic can restart rather than just dying. – hally9k Commented Feb 12, 2018 at 20:34
  • 1 Please provide a concrete example of what you are trying to acplish. What you are asking doesn't make sense right now. – zero298 Commented Feb 12, 2018 at 20:52
 |  Show 2 more ments

3 Answers 3

Reset to default 2

Return an object that contains the value you are interested in, and an error:

return {
    value: "foo",
    error: new Error("bar")
}

The receiver can then throw the error as necessary

You can't do both. Throwing an error will either transfer control to a catch statement (at which point it's not thrown anymore), or lacking a catch, will terminate execution of the program.

More here: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/throw

When an Error is thrown, the code below the thrown line doesn't execute.

Hence, its impossible to return a value immediately after throwing an error. However, you can send a value along with the error and catch it later.

So,I am Assuming that you want to throw a value with an error...Here's how to do that...

Hope this is somewhat helpful

function UserException(message) {
    this.message = message;
    this.name = 'UserException';
  }
  
  // Make the exception convert to a pretty string when used as a string 
  // (e.g., by the error console)
  UserException.prototype.toString = function() {
    return `${this.name}: "${this.message}"`;
  }

let v = 1010 //
let val = ((e, v) => {
        setTimeout(() => {
      console.log("value from error"+" :"+e.message,",variable v"+" :"+v);
      throw(e);
    }, 0); // will throw in console
})(new UserException(v), v) //

本文标签: How can you both throw an Error and return a value from a Javascript functionStack Overflow