简体   繁体   English

除了使用函数外,还有什么方法可以从表达式中引发异常?

[英]Is there way to throw an exception from an expression besides using functions?

Is there a way to explicitly throw an exception from an expression in JavaScript besides placing that throw a function wrapper? 除了放置throw函数包装的方法外,是否有办法从JavaScript表达式中显式引发异常?

throw is a statement, statements don't produce values, thus they cannot be used in expressions which operate on values and always produce a value. throw是一个语句,语句不产生值,因此不能在对值进行运算并始终产生值的表达式中使用它们。 It's quite obvious that any statement can be put to a function and a function call can be used from the expression. 很明显,任何语句都可以放在函数中,并且可以从表达式中使用函数调用。 Is there a way to avoid wrapping the throw statement to a function and throw from the expression directly? 有没有一种方法可以避免将throw语句包装到函数并直接从表达式中抛出?

UPDATE: 更新:

Some people say it's not quite clear what I am asking about. 有人说我在问什么不清楚。 Consider a hypothetical example (not for real), this is something I wish worked, but it doesn't: 考虑一个假设的例子(不是真实的例子),这是我希望起作用的东西,但它没有:

var one = value != null ? value * value : (throw new Error('The value has not been provided'));

var two = value != 0 ? 1 / value : (throw new Error('The result cannot be evaluated because the value is zero which would give a division by zero problem.));

No. As you say, throw is a statement and statements cannot appear as parts of an expression. 否。正如您所说, throw是一个语句,语句不能作为表达式的一部分出现。

The only way to execute a statement from the evaluation of an expression are a function call or eval . 从表达式求值执行语句的唯一方法是函数调用或eval Using a THROW(new Error(…)) function is probably the best you can get. 使用THROW(new Error(…))函数可能是最好的选择。

But seriosly, go with simple, readable statements: 但有趣的是,请使用简单易读的语句:

if (value == null) throw new Error('The value has not been provided');
var one = value * value;

if (value == 0) throw new Error('division by zero");
var two = 1 / value;

That will not only work, but also fit in a line. 这不仅行得通,而且还可以排成一行。

Yes you can throw from a ternary! 是的,你可以扔三元! Perhaps some might call my solution a bit of a hack, but it is very simple to reason about. 也许有些人可能将我的解决方案称为hack,但是推理起来很简单。

throw from a ternary 从三元抛出

var two = value != 0 ? 1 / value : (void 0).throwDivideByZero()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Javascript 表达式中使用 throw - Using throw in a Javascript expression HTML5从iFrame调用父JavaScript函数的方法 - 除了使用postMessage之外? 或PhoneGap应用程序的解决方案? - HTML5 way of invoking parent JavaScript function from iFrame — besides using postMessage? Or solution for PhoneGap apps? 从javascript内部抛出异常 - Throw an exception from inside javascript 期望表达式不会使用 Chai 抛出任何错误 - Expect expression not to throw any error using Chai React Router-除了使用Context之外,还有一种“更轻松”的方法来路由到新路径吗? - React Router - Is there an 'easier' way to route to a new path besides using Context? JS:我可以在不使用 throw 的情况下从嵌套函数中提前返回吗 - JS: Can I return early from nested functions without using throw 如何从异步函数中抛出异常 - How to throw exception from async function 从异步方法抛出异常是否正常? - Is it normal to throw exception from asynchronous methods? 如何使用 wso2 中的 IN 序列中的 javascript 引发异常 - How can i throw exception using javascript from IN sequence in wso2 在多个函数中抛出相同TypeError的DRY方法 - DRY way to throw the same TypeError in more than one functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM