将多个异常类型匹配到同一案例
当将异常与 case 语句进行模式匹配时,是否有更简化的方法将相同的异常与一组异常类型匹配?而不是这样:
} catch {
case e if e.isInstanceOf[MappingException] || e.isInstanceOf[ParseException] =>
这样的事情会很好:
case e: MappingException | ParseException | SomeOtherException =>
这样的事情可能吗?
When pattern matching an exception with a case statement, is there a more simplified way of matching the same exception to a set of exception types? Instead of this:
} catch {
case e if e.isInstanceOf[MappingException] || e.isInstanceOf[ParseException] =>
Something like this would be nice:
case e: MappingException | ParseException | SomeOtherException =>
Is something like this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以这样做:
如果您尝试保存一些代码行并且定期处理相同类型的异常,您可以考虑预先定义一个部分函数来用作处理程序:
You can do this:
If you're trying to save some lines of code and you handle the same types of exceptions regularly, you might consider defining a partial function beforehand to use as a handler: