javascript javascript中的多个捕获

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33781818/
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-10-28 16:53:36  来源:igfitidea点击:

Multiple catch in javascript

javascript

提问by Daniel A. White

Is it possible to use multiple catch in JS(ES5 or ES6)like I describe below (it is only example):

是否可以S(ES5 or ES6)像我在下面描述的那样在 J 中使用多个捕获(这只是示例):

try {
----  // just an error
  throw 1; 
}
catch(e if e instanceof ReferenceError) {
----  // here i would like to manage errors which is 'undefined' type
}
catch(e if typeof e === "string") {
  ----// here i can manage all string exeptions
}
// and so on and so on
catch(e) {
  ----// and finally here i can manage another exeptions
}
finally {
----  // and a simple finally block
}

This is the same as we have in C#or in a Java.

这与我们在C#或 中的相同Java

Thanks in advance!

提前致谢!

回答by Daniel A. White

No. That does not exist in JavaScript or EcmaScript.

不。这在 JavaScript 或 EcmaScript 中不存在。

You can accomplish the same thing with an if[...else if]...elseinside of the catch.

你可以做到相同的事情与if[...else if]...else内部catch

There are some non-standard implementations (and are not on any standard track) that do have it according to MDN.

根据 MDN ,有一些非标准实现(并且不在任何标准轨道上)确实具有它

回答by Legotin

Try in a that way:

以这种方式尝试:

try {
  throw 1; 
}
catch(e) {
    if (e instanceof ReferenceError) {
       // ReferenceError action here
    } else if (typeof e === "string") {
       // error as a string action here
    } else {
       // General error here
    }
}
finally {}

回答by Geek Stocks

There is absolutely nothing wrong with multiple if/then/else of course, but I never liked the look of it. I find that my eyes skim a bit faster with everything lined up, so I use the switch approach instead to help me skim/seek to the correct block. I've also started using a lexical scope {}to enclose case blocks now that the ES6 letcommand has gained popularity.

当然,多个 if/then/else 绝对没有错,但我从不喜欢它的外观。我发现所有东西都排列好后,我的眼睛浏览得更快了,所以我改用切换方法来帮助我浏览/寻找正确的块。{}既然 ES6let命令已经流行起来,我也开始使用词法范围来包含 case 块。

try {

  // OOPS!

} catch (error) {

  switch (true) {
    case (error instanceof ForbiddenError): {
      // be mean and gruff; 
      break;
    }
    case (error instanceof UserError): {
      // be nice; 
      break;
    }
    default: {
      // log, cuz this is weird;
    }
  }

}

回答by ANR Upgraded Version

This Kind of Multiple Catch we call in javascript as Conditional catch clauses

我们在 javascript 中将这种多重捕获称为条件捕获子句

You can also use one or more conditional catch clauses to handle specific exceptions. In this case, the appropriate catch clause is entered when the specified exception is thrown. As below

您还可以使用一个或多个条件 catch 子句来处理特定的异常。在这种情况下,在抛出指定的异常时输入适当的 catch 子句。如下

try {
    myroutine(); // may throw three types of exceptions
} catch (e if e instanceof TypeError) {
    // statements to handle TypeError exceptions
} catch (e if e instanceof RangeError) {
    // statements to handle RangeError exceptions
} catch (e if e instanceof EvalError) {
    // statements to handle EvalError exceptions
} catch (e) {
    // statements to handle any unspecified exceptions
    logMyErrors(e); // pass exception object to error handler
}

Non-standard: But This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

非标准:但此功能是非标准的,不在标准轨道上。不要在面向 Web 的生产站点上使用它:它不适用于每个用户。实现之间也可能存在很大的不兼容性,未来可能会改变行为。

Reference

参考