异常处理
导致程序的正常流程被中断的事件,叫做异常
try catch
- 将可能抛出FileNotFoundException 文件不存在异常的代码放在try里
- 如果文件存在,就会顺序往下执行,并且不执行catch块中的代码
- 如果文件不存在,try 里的代码会立即终止,程序流程会运行到对应的catch块中
- e.printStackTrace(); 会打印出方法的调用痕迹
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class TestException {
public static void main(String[] args) {
File f= new File("d:/test.exe");
try{
System.out.println("试图打开 d:/test.exe");
new FileInputStream(f);
System.out.println("成功打开");
}
catch(FileNotFoundException e){
System.out.println("d:/test.exe不存在");
e.printStackTrace();
}
}
}
FileNotFoundException是Exception的子类,使用Exception也可以catch住FileNotFoundException
多异常捕捉办法
- 对每个异常分别进行catch
try {
System.out.println("试图打开 d:/test.exe");
new FileInputStream(f);
System.out.println("成功打开");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse("2016-06-03");
}
catch (FileNotFoundException e) {
System.out.println("d:/test.exe不存在");
e.printStackTrace();
} catch (ParseException e) {
System.out.println("日期格式解析错误");
e.printStackTrace();
}
- 把多个异常,放在一个catch里统一捕捉
catch (FileNotFoundException | ParseException e)
这种方式从 JDK7开始支持,好处是捕捉的代码更紧凑,不足之处是,一旦发生异常,不能确定到底是哪种异常,需要通过instanceof 进行判断具体的异常类型
if (e instanceof FileNotFoundException)
System.out.println("d:/testq.exe不存在");
if (e instanceof ParseException)
System.out.println("日期格式解析错误");
finally
无论是否出现异常,finally中的代码都会被执行
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class TestException {
public static void main(String[] args) {
File f= new File("d:/test.exe");
try{
System.out.println("试图打开 d:/test.exe");
new FileInputStream(f);
System.out.println("成功打开");
}
catch(FileNotFoundException e){
System.out.println("d:/test.exe不存在");
e.printStackTrace();
}
finally {
System.out.println("always be there");
}
}
}
throw
考虑如下情况: 主方法调用method1 method1调用method2 method2中打开文件
method2中需要进行异常处理 但是method2不打算处理,而是把这个异常通过throws抛出去 那么method1就会接到该异常。 处理办法也是两种,要么是try catch处理掉,要么也是抛出去。 method1选择本地try catch住 一旦try catch住了,就相当于把这个异常消化掉了,主方法在调用method1的时候,就不需要进行异常处理了
throws与throw这两个关键字接近,不过意义不一样,有如下区别:
throws 出现在方法声明上,而throw通常都出现在方法体内。
throws 表示出现异常的一种可能性,并不一定会发生这些异常,throw则是抛出了异常,执行throw则一定抛出了某个异常对象。
异常分类
异常分类:
- 可查异常
- 运行时异常
- 错误
可查异常: CheckedException 可查异常即必须进行处理的异常,要么try catch住,要么往外抛,谁调用,谁处理,比如 FileNotFoundException 如果不处理,编译器,就不让你通过
运行时异常(非可查异常)
运行时异常RuntimeException指: 不是必须进行try catch的异常 常见运行时异常: 除数不能为0异常:ArithmeticException 下标越界异常:ArrayIndexOutOfBoundsException 空指针异常:NullPointerException 在编写代码的时候,依然可以使用try catch throws进行处理,与可查异常不同之处在于,即便不进行try catch,也不会有编译错误 Java之所以会设计运行时异常的原因之一,是因为下标越界,空指针这些运行时异常太过于普遍,如果都需要进 行捕捉,代码的可读性就会变得很糟糕。
- 错误Error
指的是系统级别的异常,通常是内存用光了 在默认设置下,一般java程序启动的时候,最大可以使用16m的内存 如例不停的给StringBuffer追加字符,很快就把内存使用光了。抛出OutOfMemoryError 与运行时异常一样,错误也是不要求强制捕捉的
Throwable是类,Exception和Error都继承了该类 所以在捕捉的时候,也可以使用Throwable进行捕捉 如图: 异常分Error和Exception Exception里又分运行时异常和可查异常。
自定义异常
创建自定义异常
- 提供两个构造方法
- 无参的构造方法
- 带参的 构造方法,并调用父类的对应的构造方法
- 提供两个构造方法
抛出自定义异常
- 通过throw 抛出该异常
- 当前方法通过 throws 抛出该异常
- 可以通过e.getMessage() 获取当时出错的具体原因