检查异常在运行时发生吗?
我对java运行时异常的命名感到困惑。 这些检查异常(例如 SQLException)也会在程序执行期间发生。 为什么只有那些未经检查的才称为运行时异常? 这可能是我对“运行时”的误解。
感谢您的任何建议。
I am confused with the naming of runtime exception of java.
Those checked exceptions, like SQLexception, also happen during the execution of a program.
Why only those unchecked ones called runtime exception?
It is probably I have misunderstanding of "runtime".
Thank you for any advices.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能理解你的困惑。所有异常都发生在运行时!
我能想到以这种方式命名类的唯一原因是,它澄清了这是一个不必在编译时处理的异常。
与所有其他所谓的“检查”异常相反,
RuntimeExceptions
不需要程序员声明使用throws
抛出异常条款。I can understand your confusion. All exceptions occur at runtime!
The only reason I can come up with for naming the class that way, is that it clarifies that it is an exception that doesn't have to be dealt with at compile time.
As opposed to all other so called "checked" exceptions,
RuntimeExceptions
doesn't require the programmer to declare the exception to be thrown using athrows
clause.RuntimeException
是java.lang.Exception
的子类。 RunTimeExceptions 几乎总是编程错误或/和不满足不变量的结果(Nulls 在不应该传递的时候被传递),因此您不必像 java.lang 那样捕获它们.Exception(即检查异常)。您无法捕获它们,因为运行时系统几乎无法恢复。我认为“运行时”一词仅意味着它们在程序运行时发生(显然!!),并且至关重要的是编译器不会像检查异常那样强制将检查内置到代码中。我认为这是一个很难正确命名类的例子,例如我猜他们可能默认情况下未检查异常并将其称为异常。然后将其子类化以提供 CheckedException - 每个人都将 java.lang.Exception 称为 Checked Exception,但从类名中并不清楚。但他们没有,而我们有:
RuntimeException
is a subclass ofjava.lang.Exception
.RunTimeExceptions
are almost always the result of a programming error or/and invariants not being met (Nulls being passed when they shouldn't), so you do not have to catch them likejava.lang.Exception
(which is Checked Exceptions). You don't catch them because there's little the Run Time system could do to recover.I think the phrase runtime just means they happen when the program is running (obviously!!) and crucially the compiler doesn't force checks to be built into the code as with Checked Exceptions. I think it's an example of where it's difficult to name a class appropriately e.g. I guess they could have Exceptions unchecked by default and called it Exception. Then subclassed it to provide CheckedException - everyone calls java.lang.Exception a Checked Exception, but it's not clear from the Class Name. But they didn't and we have:
是的,命名有点令人困惑,但它反映了正在处理的异常的性质,而不是其发生的时间。
所有异常都发生在运行时。但是,您被迫自己处理程序针对特定类型的异常(即 CheckedException)的行为。原因是这些类型的异常更有可能发生。示例 FileNotFoundException。
另一方面,UnchekedExceptions[又名 RuntimeExcetions] 发生的可能性较小,并且程序员在编写程序时不必被迫处理这些异常。例如:算术异常。
Yes, the naming is a bit confusing but it reflects the nature of an Exception being handled, not its time of occurrence.
All the exceptions occur at runtime. However, you are forced to handle the behavior of your program yourself for a specific type of exceptions i.e. CheckedExceptions. The reason is that these types of exceptions are more likely to occur. Example FileNotFoundException.
On the other hand, UnchekedExceptions[aka RuntimeExcetions] are less likely to occur and the programmer is not forced to handle those while writing the program. Ex: ArithmeticException.