具有与 Java 7 try-multiple-catch 块等效的编程语言?
Java 7 提供了一种在一个 catch 块中捕获多个异常的新方法,如下所示。
try
{
//stuff that causes one or more of the exceptions below.
}
catch (IOException | IllegalArgumentException | IndexOutOfRangeException ex)
{
//one of the above exceptions was thrown and caught
//this code block will run if any of the above exceptions was caught
}
还有哪些其他编程语言(如果有)具有类似的方式来捕获一个块中的多个异常,或者无需为每个异常使用 catch
块?这些语言是如何实现这种对多个异常的捕获的呢?
Java 7 features a new way to catch multiple exceptions in one catch
block, as shown below.
try
{
//stuff that causes one or more of the exceptions below.
}
catch (IOException | IllegalArgumentException | IndexOutOfRangeException ex)
{
//one of the above exceptions was thrown and caught
//this code block will run if any of the above exceptions was caught
}
What other programming languages, if any, feature a similar way to capture multiple exceptions in one block, or remove the need to use a catch
block for each exception? How do these languages implement this capture of multiple exceptions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Ada 编程语言允许捕获多个异常块,但我不知道这是如何实现的 - 但应该非常有趣,因为 Ada 是非常强类型的。您可以在此处检查语法:带注释的 Ada 参考手册
The Ada programming language allows for the capture of multiple exception blocks, but I have no idea how this is implemented - but should be really interesting to know since Ada is VERY strongly typed. You can check the syntax here: Annotated Ada Reference Manual
Javascript 要求您在一个
catch
块中捕获所有异常,因为它不是静态类型的。Javascript requires you to catch all exceptions in one
catch
block, since it's not statically typed.