异常
try catch- .将可能抛出FileNotFoundException 文件不存在异常的代码放在try里
- .如果文件存在,就会顺序往下执行,并且不执行catch块中的代码
- . 如果文件不存在,try 里的代码会立即终止,程序流程会运行到对应的catch块中
- e.printStackTrace(); 会打印出方法的调用痕迹
public static void main(String[] args) {
File file = new File("E:\文本文档.txt");
try {
System.out.println("```");
new FileInputStream(file);
} catch (FileNotFoundException e) {
System.out.println("系统文件不存在");
e.printStackTrace();
}
}
使用异常的父类进行catch
public static void main(String[] args) {
File file = new File("E:\文本文档.txt");
try {
System.out.println("```");
new FileInputStream(file);
} catch (Exception e) { //FileNotFoundException是Exception的子类,使用Exception也可以catch住FileNotFoundException
System.out.println("系统文件不存在");
e.printStackTrace();
}
}
多异常捕捉办法
办法是把多个异常,放在一个catch里统一捕捉
这种方式从 JDK7开始支持,好处是捕捉的代码更紧凑,不足之处是,一旦发生异常,不能确定到底是哪种异常,需要通过instanceof进行判断具体的异常类型
public static void main(String[] args) {
File file = new File("E:\文本文档.txt");
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
simpleDateFormat.parse("2022-10-20.200");
new FileInputStream(file);
} catch (Exception e) { //FileNotFoundException是Exception的子类,使用Exception也可以catch住FileNotFoundException
if (e instanceof FileNotFoundException) System.out.println("不存在");;
if (e instanceof ParseException) System.out.println("日期解析错误");
e.printStackTrace();
}
}
finally
无论是否出现异常,finally中的代码都会被执行
public static void main(String[] args) {
File file = new File("E:\文本文档.txt");
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
simpleDateFormat.parse("2022-10-20.200");
new FileInputStream(file);
} catch (Exception e) { //FileNotFoundException是Exception的子类,使用Exception也可以catch住FileNotFoundException
if (e instanceof FileNotFoundException) System.out.println("不存在");;
if (e instanceof ParseException) System.out.println("日期解析错误");
e.printStackTrace();
}finally {
System.out.println("无论是否存在 都会执行");
}
}
throws
主方法调用method1
method1调用method2
method2中打开文件
method2中需要进行异常处理
但是method2不打算处理,而是把这个异常通过throws抛出去
那么method1就会接到该异常。 处理办法也是两种,要么是try catch处理掉,要么也是抛出去。
method1选择本地try catch住 一旦try catch住了,就相当于把这个异常消化掉了,主方法在调用method1的时候,就不需要进行异常处理了
public static void main(String[] args) {
method1();
}
private static void method1() {
try {
method2();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void method2() throws FileNotFoundException {
File f = new File("d:/LOL.exe");
System.out.println("试图打开 d:/LOL.exe");
new FileInputStream(f);
System.out.println("成功打开");
}
throw和throws的区别
throws与throw这两个关键字接近,不过意义不一样,有如下区别: 1. throws 出现在方法声明上,而throw通常都出现在方法体内。 2. throws 表示出现异常的一种可能性,并不一定会发生这些异常;throw则是抛出了异常,执行throw则一定抛出了某个异常对象。



