为什么运行时异常未检查(而不是已检查)是否有意义?
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?
发布评论