检查异常在运行时发生吗?

发布于 2024-11-16 11:22:39 字数 119 浏览 7 评论 0原文

我对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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小矜持 2024-11-23 11:22:39

我能理解你的困惑。所有异常都发生在运行时!

我能想到以这种方式命名类的唯一原因是,它澄清了这是一个不必在编译时处理的异常。

与所有其他所谓的“检查”异常相反,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 a throws clause.

满意归宿 2024-11-23 11:22:39

RuntimeExceptionjava.lang.Exception 的子类。 RunTimeExceptions 几乎总是编程错误或/和不满足不变量的结果(Nulls 在不应该传递的时候被传递),因此您不必像 java.lang 那样捕获它们.Exception(即检查异常)。您无法捕获它们,因为运行时系统几乎无法恢复。

我认为“运行时”一词仅意味着它们在程序运行时发生(显然!!),并且至关重要的是编译器不会像检查异常那样强制将检查内置到代码中。我认为这是一个很难正确命名类的例子,例如我猜他们可能默认情况下未检查异常并将其称为异常。然后将其子类化以提供 CheckedException - 每个人都将 java.lang.Exception 称为 Checked Exception,但从类名中并不清楚。但他们没有,而我们有:

> java.lang.Exception is referred to as "Checked Exception" 
> java.lang.RuntimeException is referred to as "Unchecked Exception"

RuntimeException is a subclass of java.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 like java.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:

> java.lang.Exception is referred to as "Checked Exception" 
> java.lang.RuntimeException is referred to as "Unchecked Exception"
孤独岁月 2024-11-23 11:22:39

是的,命名有点令人困惑,但它反映了正在处理的异常的性质,而不是其发生的时间。

所有异常都发生在运行时。但是,您被迫自己处理程序针对特定类型的异常(即 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文