从类的继承结构上看,Exception 和 Error 都继承与 java.lang.Throwable。
对于 Error ,API 的解释为:
An Error is a subclass of Throwable that indicates serious problems that a
reasonable application should not try to catch. Most such errors are abnormal
conditions. The ThreadDeath error, though a
"normal" condition, is also a subclass of Error because most
applications should not try to catch it.
A method is not
required to declare in its throws clause any subclasses of Error that might be
thrown during the execution of the method but not caught, since these errors
are abnormal conditions that should never occur. That is, Error and its subclasses
are regarded as unchecked exceptions for the purposes of compile-time checking
of exceptions.
对于 Exception,API 的解释为:
he class Exception and its subclasses are a form of Throwable that indicates conditions that a
reasonable application might want to catch.
The class Exception and any subclasses
that are not also subclasses of RuntimeException
are checked exceptions. Checked exceptions
need to be declared in a method or constructor's throws clause if they can be
thrown by the execution of the method or constructor and propagate outside the
method or constructor boundary.
在对 Exception 的解释中,可以看到 Exception 分为两类,一类是 checked Exception,即除 RuntimeException 及其子类之外的所有 Exception,常见的有 IOException,SQLException;另一类是 unchecked Exception,即 RuntimeException,常见的有 NullPointerException,ArithmeticException。
checkedException 是需要在代码中显示地声明或者捕捉,而 uncheckedException 则不需要。至于原因,在 SCJP 的学习指导中有较好的阐述。
在 SCJP 1.6 的学习指导中,对 java 的Exception 和 Error 作如下的划分:
JVM exceptions Those exceptions or errors that are either exclusively or most logically thrown by the JVM.
Programmatic exceptions Those exceptions that are thrown explicitly by application and/or API programmers.
对于 JVM exceptions,编译器无法在程序运行前发现可能导致这类异常产生的问题。对于 RuntimeException,有一个很好的例子:
static String str ;
public static void main(String[] args) {
System.out.println(str.length());
}
虽然这段代码明显不合理,但却可以编译通过。对 str 调用 length 方法时,将产生 NullPointerException。而对于 Error ,也有一个简单的例子:
public class Bunnies {
void go(){
go();
}
public static void main(String[] args) {
new Bunnies().go();
}
}
运行之后,JVM 将产生 java.lang.StackOverflowError 。
分享到:
相关推荐
Java中Error与Exception的区别 Java 中的 Error 和 Exception 是两个不同的...Error 和 Exception 是 Java 中两个不同的概念,它们之间存在着许多不同之处,了解它们的区别对于我们编写高质量的 Java 程序非常重要。
Java中Error和Exception的区别.pdf 学习资料 复习资料 教学资源
第一,理解 Throwable、Exception、Error 的设计和分类 第二,理解 Java 语言中操作 Throwable 的元素和实践 第一,尽量不要
第一,理解 Throwable、Exception、Error 的设计和分类 第二,理解 Java 语言中操作 Throwable 的元素和实践 第一,尽量不要
java 异常 问题收集 Exception
Java 异常机制包括 Error、Exception、检查异常、运行时异常、异常处理和 throws 关键字等几个方面。 Error 是 Java 异常机制中的基类,表示编译时或者系统错误,例如虚拟机相关的错误、系统崩溃等。Error 无法恢复...
Exception: 1.可以是可被控制(checked) 或不可控制的(unchecked)。 2.表示一个由程序员导致的错误。 3.应该在应用程序级被处理。
在oracle里面运行一下,解决Exception java.sql.SQLException ORA-00600 内部错误代码
在 Java 中的基本理念是 结构不佳的代码不能运行...我们写 Java 程序经常会出现两种问题,一种是 java.lang.Exception ,一种是 java.lang.Error,都用来表示出现了异常情况,下面就针对这两种概念进行理解。 认识 Exc
2. 运行时异常(RuntimeException) 3. 被检查的异常(Checked Exception) 1.增强程序堆内存的处理效率 2.设置缓存大小
Exception 和 Error 有什么区别? Checked Exception 和 Unchecked Exception 有什么区别? Throwable 类常用方法有哪些? try-catch-finally 如何使用? finally 中的代码一定会执行吗? 如何使用 try-with-...
error与exception的区别查阅网上资料大多是java的解释,貌似php的异常处理过程与java差不多 java中的Object继承结构如下: Object—->Throwable——–> Exception —-> RuntimeException | Error Error都是unchecked...
在 Java 中,Socket 是一个类,它提供了创建连接、发送和接收数据的方法。 * SocketException:SocketException 是一种 Java 异常,它发生在 Socket 编程中,包括连接断开、数据发送失败等情况。 * Connection reset...
Throwable 类有两个子类:Exception 和 Error。Exception 类是可以被捕捉并且可能恢复的异常类,可以继承 Exception 类生成自己的异常类。Error 类一般被认为是不可恢复和不可捕捉的异常类。 Java 异常处理可以分为...
C 与Java异常处理机制的比较,鲍鹏,高珩,本文分别深入讨论了C 与Java的异常处理机制。比较了C 与Java的异常匹配处理,异常类继承体系与异常处理代码的区别,并且讨论了异常�
通常来说,大家都是对Java中的Exception进行捕获和进行相应的处理,有些人说,error就无法捕获了。其实,error也是可以捕获的。Error和Exception都是Throwable的子类。既然可以catch Throwable,那么error也是可以...
所有的checked exception是从java.lang.Exception类衍生出来的,而runtime exception则是从java.lang.RuntimeException或java.lang.Error类衍生出来的。 它们的不同之处表现在两方面:机制上和逻辑上。 一...
1. Error 和 Exception 区别是什么? 2. 运行时异常和一般异常(受检异常)区别是什么? 3. JVM 是如何处理异常的? 4. throw 和 throws 的区别是什么? 5. final、finally、finalize 有什么区别? 6. ...
1. Error 和 Exception 区别是什么? 2. 运行时异常和一般异常(受检异常)区别是什么? 3. JVM 是如何处理异常的? 4. throw 和 throws 的区别是什么? 5. final、finally、finalize 有什么区别? 6. ...
如果java提供的系统异常类型不能满足程序设计的需求,那么可以设计自己的异常类型。 从java异常类的结构层次...另外一种问题是程序运行错误,java定义为Exception,这种情况下,可以通过程序设计调整来实现异常处理。