为什么我们需要错误类? [英] Why do we need Error class?
问题描述
我们有 Throwable
类是的基类,错误
类(for unrecoverable 错误)和异常
类(对于可恢复错误)
所以, p>
1> 我们可以 throw
code> error class(尽管实现错误
类没有意义,因为我们有异常
类做同样的事情..)
2> Java不建议抓住错误
对象..
那么需要什么错误
对象呢?编译器不能在内部实现?这不是一个错误吗?
从技术上讲,不可恢复的错误和可恢复的错误 ,但在检查的异常和未检查的异常之间。 Java 不区分它们,如下所示:
- 你必须声明一个已检查的异常在
throws
子句中;如果使用在try
块中引发检查异常的方法,则您必须catch
表示异常或添加此异常到你的方法的throws
子句; - 你可以在
throws
子句(不推荐);如果使用在尝试
块中引发未检查异常的方法,您可以catch
该异常或将此异常添加到您的方法的throws
子句中(不推荐使用)。
什么是肯定不推荐,除非你真的知道你在做什么,是吞下任何种类的未经检查的异常(即 catch
它有一个空的块)。
异常
是基本检查的异常类; 错误
和 RuntimeException
都是未检查的异常,所有的子类也是这样。您将注意到,所有三个类都扩展了 Throwable
,而 Throwable
的javadoc表示:
为了编译时检查异常,Throwable和
Throwable的任何子类都不是
的子类RuntimeException或错误被视为已检查的异常。
(in)着名的未检查异常的典型示例:
-
OutOfMemoryError
(extendsError
); -
StackOverflowError
(extendsError
); -
NullPointerException
(extendsRuntimeException
); -
IllegalArgumentException
(extendsRuntimeException
); - 等等
错误
和 RuntimeException
之间的唯一真正区别是其估计的严重性级别,是一个sem antic的区别,不是一个技术上的区别:最终,两者都是一样的。一些IDE(Intellij IDEA想到)如果你抓到一个错误
,但是不要重新抛出它,也会对你大喊。
We have Throwable
class which is the base class for Error
class (for unrecoverable errors) and Exception
class(for recoverable errors)
So,
1> we can throw
an object that implement error
class.(Although it doesn't make sense to implement Error
class because we have Exception
class to do the same thing..)
2> Java doesn't recommend to catch Error
object..
So what is the need of Error
object then? Cant the compiler implement it internally? Isn't it a mistake?
Technically, the distinction is not really made between "unrecoverable error" and "recoverable error", but between checked exceptions and unchecked exceptions. Java does distinguish between them as follows:
- you must declare a checked exception in your
throws
clause; if using a method which throws a checked exception in atry
block, you must eithercatch
said exception or add this exception to your method'sthrows
clause; - you may declare an unchecked exception in your
throws
clause (not recommended); if using a method which throws an unchecked exception in atry
block, you maycatch
that exception or add this exception to your method'sthrows
clause (not recommended either).
What is certainly not recommended, unless you really know what you are doing, is to "swallow" any kind of unchecked exception (ie, catch
it with an empty block).
Exception
is the base checked exception class; Error
and RuntimeException
are both unchecked exceptions, and so are all their subclasses. You will note that all three classes extend Throwable
, and the javadoc for Throwable
states that:
For the purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions.
Classical examples of (in)famous unchecked exceptions:
OutOfMemoryError
(extendsError
);StackOverflowError
(extendsError
);NullPointerException
(extendsRuntimeException
);IllegalArgumentException
(extendsRuntimeException
);- etc etc.
The only real difference between Error
and RuntimeException
is their estimated severity level, and is a "semantic" difference, not a technical difference: ultimately, both behave the same. Some IDEs (Intellij IDEA comes to mind) will also yell at you if you catch an Error
but do not rethrow it.
这篇关于为什么我们需要错误类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!