为什么运行时异常是“未检查的”?在Java中?

发布于 2024-08-10 10:56:23 字数 63 浏览 4 评论 0原文

为什么运行时异常未检查有意义(而不是检查)?

Why does it make sense to have Runtime Exceptions UnChecked (as opposed to if they were Checked)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

涙—继续流 2024-08-17 10:56:23

如果不这样做,每次访问数组元素、执行除法操作和许多其他常见场景时,都必须使用 try/catch 块。

换句话说,想象一下这段代码:

Map map = ...
int i = ...
(int[])map.get("foo")[3] = 2334 / i;

必须检查 ClassCastExceptionArrayIndexOutofBoundsExceptionArithmeticExceptionUnsupportedOperationException和 NullPointerException 就在我的脑海中。

对于 Java,问题不在于未经检查的异常。检查异常是一个极具争议性的主题。有人说这主要是对 Java 的一个实验,实际上它们不起作用,但你会发现很多人认为它们很好。

然而,没有人认为未经检查的异常是不好的。

If you didn't you would have to have try/catch blocks every time you accessed an array element, did a division operation and many other common scenarios.

To put it another way, imagine this code:

Map map = ...
int i = ...
(int[])map.get("foo")[3] = 2334 / i;

would have to check for ClassCastException, ArrayIndexOutofBoundsException, ArithmeticException, UnsupportedOperationException and NullPointerException just off the top of my head.

With Java the issue isn't unchecked exceptions. Checked exceptions are a highly controversial subject. Some say this was largely an experiment with Java and in practice they don't work but you will find plenty of people who argue they are good.

No one is arguing unchecked exceptions are bad however.

不念旧人 2024-08-17 10:56:23

Java 中两种异常(受检异常和非受检异常)的思想是,受检异常应用于可以合理预期发生的错误情况,而非受检异常应用于意外错误情况。

例如,如果未找到文件,您将收到 FileNotFoundException,并且可以合理地期望您的程序能够处理此类情况。未经检查的异常应该只用于不应该发生的问题,这实际上意味着如果发生这样的问题,程序中存在错误。例如,NullPointerException 意味着您的程序正在尝试取消引用 null 的变量,这很可能是一个错误。

Java 编译器强制程序员处理检查异常。这使得编程语言更安全——这意味着程序员被迫考虑错误条件,这应该使程序更加健壮。

编译器不会检查未经检查的异常,因为未经检查的异常无论如何都不应该发生,如果发生了,程序在运行时就无法合理地执行任何操作;程序员必须解决这个错误。

Java中的这个特性受到了一些批评,有些人甚至称检查异常失败的实验和一些人建议删除选中的Java 的异常

The idea of the two kinds of exceptions in Java (checked and unchecked) is that checked exceptions should be used for error conditions that can reasonably be expected to happen, and unchecked exceptions should be used for unexpected error conditions.

For example if a file isn't found, you get a FileNotFoundException, and it's reasonable to expect your program to be able to handle such a condition. Unchecked exceptions should be used only for problems that shouldn't happen, and that really mean that there is a bug in the program if such a problem happens. For example, a NullPointerException means that your program is trying to dereference a variable that is null and that's most likely a bug.

The Java compiler forces the programmer to handle checked exceptions. This makes the programming language safer - it means that the programmer is forced to think about error conditions, which should make the program more robust.

The compiler doesn't check unchecked exceptions, because unchecked exceptions aren't supposed to happen anyway and if they do, there's nothing that the program could reasonably do at runtime; the programmer must solve the bug.

There has been some criticism to this feature in Java, some people even call checked exceptions a failed experiment and some people propose to remove checked exceptions from Java.

甜是你 2024-08-17 10:56:23

这仅仅意味着编译器不会强迫您寻找异常,但您仍然可以在运行时抛出它。好处之一是,这允许您从类中抛出新的异常,而无需更改接口,从而导致调用者更改其代码。

This simply means that the compiler will not force you to look for an exception, but you can still throw it at runtime. As one benefit, this allows you to throw new exceptions from your classes without requiring you to alter your interface, causing callers to change their code.

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