为什么运行时异常“未选中”?在Java?

编程入门 行业动态 更新时间:2024-10-23 22:22:42
本文介绍了为什么运行时异常“未选中”?在Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

推荐答案

如果不这样做,则每次访问数组元素时都必须使用try / catch块,执行除法运算和许多其他常见方案。

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;

必须检查 ClassCastException , ArrayIndexOutofBoundsException , ArithmeticException , UnsupportedOperationException 和 NullPointerException 就在我的脑海中。

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

使用Java时,问题不是未经检查的异常。检查异常是一个备受争议的主题。有人说这主要是用Java进行的实验,在实践中它们不起作用,但你会发现很多人认为它们很好。

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.

更多推荐

为什么运行时异常“未选中”?在Java?

本文发布于:2023-10-31 04:59:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545073.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!