error和exception区别

Throwable 类是 Java 语言中所有错误或异常的超类。它的两个子类是Error和Exception;

java.lang.Error class represents the errors which are mainly caused by the environment in which application is running. For example, OutOfMemoryError occurs when JVM runs out of memory or StackOverflowError occurs when stack overflows.

(error主要是由程序运行期间所处的运行环境引起的)

 java.lang.Exception class represents the exceptions which are mainly caused by the application itself. For example, NullPointerException occurs when an application tries to access null object or ClassCastException occurs when an application tries to cast incompatible class types. In this article, we will discuss the differences between Error and Exception in java.

(exception主要是由程序自行引起的异常)

Error(StackOverFlowError, OutOfMemoryError)的特点为:

1、不可能从错误中恢复

2、所有的error都是Error的子类

3、error一般不被编译器所知,他们经常出现在程序运行期间

4、error一般是由程序运行期间的运行环境所致

Exception的特点

1、exception分为checkedexception和uncheckedexception(RuntimeException)

2、Exception是由程序自身引起的

3、checkedexception被编译器知道然而uncheckedexception不被编译器所知

4、程序可以从exception中恢复

网易题:

NoClassFoundException和NoClassDefFoundError的区别

NoClassDefFoundError是指源程序成功的编译,但是在运行期间所需要的class类无法找到,可能是由于程序运行环境中的jar文件没有将所有的class类引入

NoClassFoundException是指程序在运行期间通过反射加载特定类名的类时,该类没有找到

两者的区别是一个是error一个是exception。NoClassDefFoundError是指jvm无法加载一个class类文件出现的错误

NoClassFoundException是一个exception,它是可预期的并且一定情况下可以恢复

CheckedEdxception和UnCheckedException区别

Checked Exceptions Unchecked Exceptions(RunTimeException)
They are known at compile time. They are known at run time.
They are checked at compile time. They are not checked at compile time. Because they occur only at run time.
These are compile time exceptions. These are run time exceptions.
If  these exceptions are not handled properly in the application, they give compile time error. If these exceptions are not handled properly, they don’t give compile time error. But application will be terminated prematurely at run time.
All sub classes of java.lang.Exception Class except sub classes of RunTimeException are checked exceptions. All sub classes of RunTimeException and sub classes of java.lang.Error are unchecked exceptions.

posted @ 2016-03-24 00:37  程序猿进化之路  阅读(228)  评论(0)    收藏  举报