我为什么要扩展java.lang.RuntimeException [英] Why would I extend java.lang.RuntimeException

查看:45
本文介绍了我为什么要扩展java.lang.RuntimeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近有人在采访中问我一个有关扩展java.lang.RuntimeException的问题.

I was recently asked a question, apparently in an Interview, about extending the java.lang.RuntimeException.

有人要求我举一个需要扩展java.lang.RuntimeException的示例.

I was asked to cite an example where there would be a need to extend the java.lang.RuntimeException.

我一直以为我们不需要扩展RuntimeException,有人可以启发我吗?

I always thought that we would not need to extend the RuntimeException, can anybody enlighten me?

谢谢,SB

推荐答案

一层的运行时异常是另一层的已检查(并已执行)异常.

One layer's runtime exception is another layer's checked (and acted upon) exception.

我可以看到例如servlet容器;REPL和/或任何顶级解释器循环;等合法选择和捕获RuntimeException,因为它们不应仅仅因为某些东西在堆栈中更深入而崩溃.

I can see containers e.g. servlet container; REPLs and/or anything top-level interpreter loop; etc. legitimately selecting and catching RuntimeExceptions as they are not supposed to crash simply because something goes boom deeper in the stack.

类似于容器的情况,跨越邻接边界,例如跨层或跨层通常要求更加明确的异常语义.

Similar to the container case, crossing adjacency boundaries e.g. crossing layers or tiers typically calls for a more articulated exception semantics.

如果原因"和/或消息属性在语义上不足以表示嘿,它坏了"以外的任何内容,并且客户端/高层可以选择性地执行操作,则可以将RuntimeException子类化是合法的.

If the 'cause' and/or message properties prove semantically insufficient to express anything beyond "hey, it broke", and the client/higher layer can act selectively, then it is arguably legitimate to sub-class RuntimeException.

这篇关于我为什么要扩展java.lang.RuntimeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆