1.异常介绍:
用来描述代码中出现异常(也就问题)的对象
(1)可以在方法中处理异常
(2)也可以将异常进行向上传递
2.异常相关关键字
try catch throw throws finally
3.异常代码块结构
try{
}catch(Exception1 e1){
}catch(Exception2 e2){
}...
finally{
//此处代码一定会执行
}
java 异常的继承关系和分类
主要有以下对象 Throwable Error Exception RunntimeException
继承关系
Exception 和 Error :
Exception :
- 用于程序异常捕获
- 用于程序自定义异常
RunntimeException
运行时异常,异常是自动定义的。举例:
具体可查看api文档
AnnotationTypeMismatchException,
ArithmeticException,
ArrayStoreException,
BufferOverflowException,
BufferUnderflowException
Error
1.常规情况下由程序捕获的异常
2.通常响应系统灾难性的失败而创建的
举例
AnnotationFormatError,
AssertionError, AWTError,
CoderMalfunctionError,
FactoryConfigurationError
未捕获的异常
如果没有提供自己的异常处理程序,异常会由运行时系统提供默认的处理程序,没有被程序捕获的异常,最终会被默认的处理程序 处理。
try catch的使用
1.自己处理异常
2.允许修复错误
3.阻止程序自动终止
4.catch 指定希望捕获的异常
嵌套try catch
示例:
try{
try{
}catch(type Exception){
...先执行第一个try 单元的异常,如果捕获不到执行外层
}
}catch(type Exception){
...内部捕获不到的外部执行,内部捕获异常后,外部不再捕获异常
}
代码示例
int[] arrs = new int[2];
try{
try{
System.out.println(10/0);
arrs[3] = 10;
}catch(ArithmeticException e1){
System.out.println("除数不能为0");
}catch(Exception e2){
System.out.println("捕获所有的异常");
}
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("数组越界");
}
throw
显示的抛出异常 throw ThrowableInstance; ThtowableInstance必须是Throwable 或者其他子类对象,基本类型和非Throwable都不能用作异常
throws
方法引发的自身不处理的异常,指明异常的对象
type method-name(parameter-list) throws exception-list exception-list 可以设置多个异常类型对象
finally
try catch 中的代码块会根据类型是否符合进行捕获,但是最后都会执行 finally 代码块
创建异常类
1.继承Throwable
2.继承Exception及其子类即可
常用的异常api
addSuppressed(Throwable exe)
添加到异常异常关联的列表中
MyException exe = new MyException("");
......
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("数组越界");
exe.addSuppressed(e); //注意这里
......
Exception in thread "main" com.tryCatch.MyException
at com.tryCatch.NestedException.testException(NestedException.java:32)
at com.tryCatch.NestedException.main(NestedException.java:6)
Suppressed: java.lang.ArrayIndexOutOfBoundsException: 3
at com.bai.tryCatch.NestedException.testException(NestedException.java:16)
其他api 列表
后期进行补充
链式异常
1.涉及到的方法
initCause(Throwable e)
Throwable getCause()
initCause 一个异常类只能使用一次,编译能通过,运行时会出错
getCause获取后边的异常类型
具体请看下边的示例:
public static void demoproc(){
NullPointerException exeNUll = new NullPointerException("空指针");
exeNUll.initCause(new ArithmeticException("除数不能为0"));
exeNUll.initCause(new ArrayIndexOutOfBoundsException("数组越界"));
throw exeNUll;
}
public static void main(String[] args) {
try{
demoproc();
}catch(NullPointerException e){
Throwable t = e.getCause();
System.out.println(t);
System.out.println(e.getCause());
}
}
运行报错
Exception in thread "main" java.lang.IllegalStateException: Can't overwrite cause with java.lang.ArrayIndexOutOfBoundsException: 数组越界
at java.lang.Throwable.initCause(Throwable.java:458)
at com.bai.tryCatch.ChainException.demoproc(ChainException.java:8)
at com.bai.tryCatch.ChainException.main(ChainException.java:15)
Caused by: java.lang.NullPointerException: 空指针
at com.bai.tryCatch.ChainException.demoproc(ChainException.java:6)
... 1 more
Caused by: java.lang.ArithmeticException: 除数不能为0
at com.bai.tryCatch.ChainException.demoproc(ChainException.java:7)
... 1 more
正确示例:
......
//exeNUll.initCause(new ArrayIndexOutOfBoundsException("数组越界"));
........
输出
java.lang.ArithmeticException: 除数不能为0
java.lang.ArithmeticException: 除数不能为0