java error_java基础:Error和Exception
一、Error和Exception什么关系
Error和Exception都继承自Throwable。继承关系如下图所示:
其实Error和Exception就是java语言设计Throwable时的两个大的分类:Error一般是比较严重的问题,不应该被catch,比如出现了OutOfMemoryError,StackOverFlowError这些错误,程序员是无能为力的;而Exception是程序员可以干预的,可以被catch。看下java源码的注释。
1. Error
/**
* An {@code Error} is a subclass of {@code Throwable}
* that indicates serious problems that a reasonable application
* should not try to catch. Most such errors are abnormal conditions.
* The {@code ThreadDeath} error, though a "normal" condition,
* is also a subclass of {@code Error} because most applications
* should not try to catch it.
*
* A method is not required to declare in its {@code throws}
* clause any subclasses of {@code 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, {@code Error} and its subclasses are regarded as unchecked
* exceptions for the purposes of compile-time checking of exceptions.
*
* @author Frank Yellin
* @see java.lang.ThreadDeath
* @jls 11.2 Compile-Time Checking of Exceptions
* @since 1.0
*/
public class Error extends Throwable
复制代码
Error就是一个比较严重的问题,不应该被catch;它是一个unchecked exception,也就是说,不需要在方法上声明throws,它不是一个编译器可以发现的checked exception。
2. Exception
/**
* The class {@code Exception} and its subclasses are a form of
* {@code Throwable} that indicates conditions that a reasonable
* application might want to catch.
*
*
The class {@code Exception} and any subclasses that are not also
* subclasses of {@link RuntimeException} are checked
* exceptions. Checked exceptions need to be declared in a
* method or constructor's {@code throws} clause if they can be thrown
* by the execution of the method or constructor and propagate outside
* the method or constructor boundary.
*
* @author Frank Yellin
* @see java.lang.Error
* @jls 11.2 Compile-Time Checking of Exceptions
* @since 1.0
*/
public class Exception extends Throwable
复制代码
Exception及其子类是可以被catch处理的。Exception及其子类(不包括RuntimeException及其子类)都是checked exception,可以被编译器发现。也就是说,如果不显示处理这些checked exception,编译无法通过。怎么处理呢?可以在方法或者构造方法上声明throws,或者增加try catch语句块。
写到这里,除了checked exception,其他都是unchecked exception。所谓checked是针对编译器而言的,只有编译器会“检查”;运行时异常和Error显然是编译器无法“检查”到的。
二、NoClassDefFoundError 和 ClassNotFoundException
这两个有什么区别。从字面上就可以看到,一个是Error,一个是Exception。看下java定义:
1. ClassNotFoundException
/**
* Thrown when an application tries to load in a class through its
* string name using:
*
*
The forName
method in class Class
.
*
The findSystemClass
method in class
* ClassLoader
.
*
The loadClass
method in class ClassLoader
.
*
*
* but no definition for the class with the specified name could be found.
*
*
* @author unascribed
* @see java.lang.Class#forName(java.lang.String)
* @see java.lang.ClassLoader#findSystemClass(java.lang.String)
* @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
* @since 1.0
*/
public class ClassNotFoundException extends ReflectiveOperationException
复制代码
可以看到,它是个反射操作异常,并不是一个RuntimeException,也就是它是一个checked exception,必须显示的throws或者try catch,否则无法编译通过。
2. NoClassDefFoundError
/**
* Thrown if the Java Virtual Machine or a ClassLoader
instance
* tries to load in the definition of a class (as part of a normal method call
* or as part of creating a new instance using the new
expression)
* and no definition of the class could be found.
*
* The searched-for class definition existed when the currently
* executing class was compiled, but the definition can no longer be
* found.
*
* @author unascribed
* @since 1.0
*/
public
class NoClassDefFoundError extends LinkageError
复制代码
NoClassDefFoundError是当new这个类的实例的时候,在JVM中找不到这个类的定义。也就是,这个类在编译的时候还是在的,都已经装载到JVM中了,但是在new实例的时候却不存在了,真是匪夷所思。
三、参考链接
硬广告
欢迎关注公众号:double6
java error_java基础:Error和Exception相关推荐
- Java异常之 Error 和 Exception
简单了解 Java 异常 1.实际工作中,遇到的情况不可能是非常完美的. 比如:你写的某个模块,用户输入不一定符合你的要求: 你的程序要打开某个文件,这个文件可能不存在或者文件格式不对: 你要读取数据 ...
- Java面试题 Error和Exception有什么区别?列出你见过的Execption并简要说明?
1.Error和Exception有什么区别? Error:表示系统级的错误和程序不必处理的异常,是恢复不是不可能但很困难的情况下的一种严重问题:比如内存溢出,不可能指望程序能处理这样的情况. Exc ...
- java 异常处理 Throwable Error 和Exception
Java异常类层次结构图: 异常的英文单词是exception,字面翻译就是"意外.例外"的意思,也就是非正常情况.事实上,异常本质上是程序上的错误,包括程序逻辑错误和 ...
- Java基础-异常(Exception)处理
Java基础-异常(Exception)处理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.异常的概述 什么是异常?Java代码在运行时期发生的问题就是异常.在Java中,把异 ...
- java exception源码_Java异常之 Error 和 Exception
简单了解 Java 异常 1.实际工作中,遇到的情况不可能是非常完美的. 比如:你写的某个模块,用户输入不一定符合你的要求: 你的程序要打开某个文件,这个文件可能不存在或者文件格式不对: 你要读取数据 ...
- Java的异常:Error与Exception
Java的异常:Error与Exception 参考文章: (1)Java的异常:Error与Exception (2)https://www.cnblogs.com/heqiyoujing/p/11 ...
- Kafka启动出现Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Pr
kafka启动时,出现报错! [root@Spark1 bin]# cat nohup.out Unrecognized VM option 'UseCompressedOops' Error: Co ...
- Java中Error和Exception的异同
Error(错误)和 Exception(异常)都是 java.lang.Throwable 类的子类,在 Java 代码中只有继承了 Throwable 类的实例才能被 throw 或者 catch ...
- java异常中的Error和Exception的区别是什么?
java异常中的Error和Exception的区别是什么? 参考文章: (1)java异常中的Error和Exception的区别是什么? (2)https://www.cnblogs.com/Tr ...
- java异常体系_Java异常体系(Throwable、Error、Exception)
Java异常体系 java异常体系继承图 Throwable Throwable类是所有异常的父类,常见的子类有两个Error.Exception. Error Error类就是程序运行时候抛出的最严 ...
最新文章
- Django Python:完整的BUNDLE + Django真实项目2021
- java关键字的用法_JAVA常用关键字及其用法简要说明
- Java并发编程中级篇(一):使用Semaphore信号量进行并发控制
- 华视读卡器多浏览器插件_翻遍Chrome商店,这9款插件值得安装
- 网页传上服务器 是乱码,前端传到后台中文乱码问题
- 概率论与数理统计思维导图
- 福州发布《关于运用大数据加强对市场主体服务和监管实施方案》
- 用牛顿法求算术平方根python
- 通信原理 | 宽带:运营商的带宽和实际网速的关系
- 编码学习——UTF-8与Unicode互转具体流程
- ROS获取串口信息及后续处理(以惯导IMU XW-GI5651为例)
- 2021年氯化工艺报名考试及氯化工艺考试内容
- php用户登入与注销(cookie)
- ArcGIS基础实验操作100例--实验24提取栅格独立格网面
- 给mysql添加用户,使其可以远程访问
- windows7下安装python的peewee模块以及使用Anaconda Navigator安装python包
- echarts饼图封装
- 阳光电源逆变器实现光伏电站无功电压控制试验成功
- 为什么说计算机内存不够,excel为什么总是内存不够
- 高校计算机组成原理实验室,计算机组成原理虚拟实验室研究与实现.PDF