异常处理的两种方式:
声明抛出 throws 声明抛出的位置:是在方法声明的位置上使用throws关键字向上抛出异常。
捕捉 try....catch..
public class ExceptionTest03{
public static void main(String[] args){
//创建文件输入流读取文件
//思考:java编译器是如何知道以下的代码执行过程中可能出现异常,
//java编译器是如何知道这个异常发生的几率比较高呢?
//java编译器不是那么智能,因为FileInputStream这个构造方法在声明的位置上使用了throws FileNotFoundException;
FileInputStream fis=new FileInputStream("c:/ab.txt");
}
}
//以上程序编译不通过
/*
ExceptionTest03.java:16: 未报告的异常 java.io.FileNotFoundException;必须对其进行捕捉或声明以便抛出 FileInputStream fis = new FileInputStream("c:/ab.txt");
*/
深入了解throws关键字
public class ExceptionTest04{
public static void main(String[] args) throws FileNotFoundException{
//m1();
//使用throws处理异常不是真正处理异常而是推卸责任。
//谁调用的就会抛给谁。
//上面的m1方法如果出现了异常,因为采用的是上抛,给了JVM,JVM遇到这个异常就会退出JVM,下面的这个代码不会执行.
//System.out.println("Hello World");
//真正处理
try{
m1();
}catch(FileNotFoundException e){}
System.out.println("Hello World");
}
public static void m1() throws FileNotFoundException{
m2();
}
public static void m2() throws FileNotFoundException{
m3();
}
public static void m3() throws FileNotFoundException{
new FileInputStream("c:/ab.txt"); //FileInputStream构造方法声明位置上使用throws(向上抛)
}
}
/*
//在程序运行过程中发生了FileNotFoundException类型的异常.
//JVM为我们创建了一个FileNotFoundException类型的对象。
//该对象中携带以下的信息。
//JVM负责将该对象的信息打印到控制台。
//并且JVM停掉了程序的运行。
Exception in thread "main" java.io.FileNotFoundException: c:\ab.txt (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at ExceptionTest04.m3(ExceptionTest04.java:31)
at ExceptionTest04.m2(ExceptionTest04.java:26)
at ExceptionTest04.m1(ExceptionTest04.java:21)
at ExceptionTest04.main(ExceptionTest04.java:11)
*/
try...catch..
语法:
try{
可能出现异常的代码;
}catch(异常类型1 变量){
处理异常的代码;
}catch(异常类型2 变量){
处理异常的代码;
}....
1.catch语句块可以写多个.
2.但是从上到下catch,必须从小类型异常到大类型异常进行捕捉。
3.try...catch...中最多执行1个catch语句块。执行结束之后try...catch...就结束了。
public class ExceptionTest05{
public static void main(String[] args){
//以下代码编译无法通过,因为FileNotFoundException没有处理.
/*
try{
//FileNotFoundException
FileInputStream fis = new FileInputStream("c:/ab.txt");
}catch(ArithmeticException e){ //捕获的异常是算术异常
}
*/
//编译通过
/*
try{
//FileNotFoundException
FileInputStream fis = new FileInputStream("c:/ab.txt");
}catch(FileNotFoundException e){
}
*/
//以下程序编译无法通过
//因为还有更多IOException没有处理.
/*
try{
//FileNotFoundException
FileInputStream fis = new FileInputStream("c:/ab.txt");
fis.read();
}catch(FileNotFoundException e){
}
*/
//编译可以通过
/*
try{
//FileNotFoundException
FileInputStream fis = new FileInputStream("c:/ab.txt");
fis.read();
}catch(FileNotFoundException e){
}catch(IOException e){
}
*/
//编译通过.
/*
try{
FileInputStream fis = new FileInputStream("c:/ab.txt");
fis.read();
}catch(IOException e){
}
*/
//编译无法通过
//catch可以写多个,但是必须从上到下,从小到大捕捉。
/*
try{
FileInputStream fis = new FileInputStream("c:/ab.txt");
fis.read();
}catch(IOException e){
}catch(FileNotFoundException e){
}
*/
}
public class ExceptionTest06{
//编译无法通过.
//IOException没有处理
/*
public static void main(String[] args) throws FileNotFoundException{
FileInputStream fis = new FileInputStream("abc");
fis.read();
}
*/
//通过
/*
public static void main(String[] args) throws FileNotFoundException,IOException{
FileInputStream fis = new FileInputStream("abc");
fis.read();
}
*/
//通过
/*
public static void main(String[] args) throws IOException{
FileInputStream fis = new FileInputStream("abc");
fis.read();
}
*/
public static void main(String[] args){
try{
//程序执行到此处发生了FileNotFoundException类型的异常.
//JVM会自动创建一个FileNotFoundException类型的对象,将该对象的内存地址赋值给catch语句块中的e变量.
FileInputStream fis = new FileInputStream("abc");
//上面的代码出现了异常,try语句块的代码不再继续执行,直接进入catch语句块中执行。
System.out.println("TTTTTTT");
fis.read();
}catch(FileNotFoundException e){ //e内存地址指向堆中的那个对象是“FileNotFoundException类型的”事件。
System.out.println("读取的文件不存在!");
//FileNotFoundException将Object中的toString方法重写。
System.out.println(e); //java.io.FileNotFoundException: abc (系统找不到指定的文件。)
}catch(IOException e){
System.out.println("其他IO异常!");
}
System.out.println("ABC");
}
}
转载于:https://blog.51cto.com/hangtiangazi/1661694