文章目录
- 为什么要有异常处理机制
- 异常的概念
- 异常的分类
- 异常的捕获和处理
- 使用自定义的异常
为什么要有异常处理机制
异常处理机制可以让程序具有极好的容错性,让程序更加健壮。
异常的概念
- 运行期出现的错误
- Java异常是Java提供的用于处理程序中错误的一种机制。
- 所谓错误是指在程序运行的过程中发生的一些异常事件(如:除0溢出,数组下标越界,所要读取的文件不存在)。
- 设计良好的程序应该在异常发生时提供处理这些错误的方法,使得程序不会因为异常的发生而阻断或产生不可预见的结果。
- Java程序的执行中如果出现异常事件,可以生成一个异常类对象,该异常对象封装了异常事件的信息并将被提交给Java运行系统,这个过程称为抛出(throw)异常。
- 当Java运行时系统接收到异常对象时,会寻找能处理这一异常的代码并把当前异常对象交给其处理,这一过程称为捕获(catch)异常。
try{System.out.println(2/0); //会创建一个:ArithmeticException类型的异常对象}catch(ArithmeticException ae){//System.out.println("系统正在维护,请与管理员联系");System.out.println("除数不能为0");ae.printStackTrace(); }
异常的分类
Java把所有非正常情况分为两种:异常(Exception)和错误(Error),都继承Throwable父类。
- Error:称为错误,一般是指与虚拟机相关的问题,这种错误无法恢复或不可能捕获,将导致应用程序中断,包括动态链接失败、虚拟机错误等,程序对其不做处理。
- Exception:所有异常的父类,其子类对应了各种各类可能出现的异常事件,一般需要用户显式的声明或捕获。
- Runtime Exception:一类特殊的异常,如被0除、数组下标超范围等,其产生比较频繁,处理麻烦。如果显式的声明或捕获,将对程序可读性和运行效率影响很大。因此由系统自动检测并将它们交给缺省的异常处理程序(用户可不必对其处理)。
异常的捕获和处理
try{//可能抛出异常的语句
}catch(SomeException1 e){......
}catch(SomeException2 e){......
}finally{......
}
try语句
- try代码段包含可能产生异常的代码。
- try代码段可能会产生并抛出一种或几种类型的异常对象,其后跟有一个或多个catch代码段分别对这些异常做相应的处理。
- 如果没有异常产生,所以的catch代码段被略过不执行。
catch语句
- 每个catch代码段声明其能处理的一种特定类型的异常并提供处理的方法。
- 当异常发生时,程序会中止当前的流程,根据获取异常的类型去执行相应的catch代码段。
- 在catch中声明的异常对象( catch(SomeException1 e))封装了异常事件发生的信息,在catch语句块中可以使用这个对象的一些方法获取这些信息。
- getMessage()方法,用来得到有关异常事件的信息。
- printStackTrace()方法,用来跟踪异常事件发生时执行堆栈的内容。
finally语句
-
无论try所指定的程序块中是否抛出异常,finally所指定的代码都要被执行。
例:语句1抛出SomeException2类型的异常和没有捕获到异常时的运行顺序
-
finally语句为异常处理提供一个统一的出口,使得在控制流程转到程序的其他部分以前,能够对程序的状态作统一的管理。
-
通常在finally语句中可以进行资源的清除工作,如:关闭打开的文件、删除临时文件等。
public class TestEx {public static void main(String[] args){FileInputStream in = null;try{in = new FileInputStream("myfile.txt");int b;b = in.read();while (b != -1){System.out.println((char) b);b = in.read();}}catch(FileNotFoundException e){e.printStackTrace();}catch(IOException e){System.out.println(e.getMessage());}finally {try{in.close();}catch(IOException e){e.printStackTrace();}}}
}
public class TestEr {public static void main(String[] args){try {TestEr te = new TestEr();te.f2(); }catch (IOException e){e.printStackTrace();}}void f() throws FileNotFoundException,IOException{FileInputStream in = new FileInputStream("myfile.txt");int b;b = in.read();while (b != -1){System.out.println((char) b);b = in.read();}}void f2() throws IOException{try {f();}catch (FileNotFoundException e){System.out.println(e.getMessage());}catch (IOException e){e.printStackTrace();} }//手动抛出异常void m(int i) throws ArithmeticException {if(i==0)throw new ArithmeticException("被除数为0");}
}
Java的异常处理机制使得异常事件沿着被调用的顺序往前寻找,直到找到符合该异常种类的异常处理程序。
将catch(IOException e)放在catch(FileNotFoundException e)前面,编译出错
基类异常(IOException)捕获语句不可以写在子类异常(FileNotFoundException)捕获语句前面
public class TestEx {public static void main(String[] args){FileInputStream in = null;try{in = new FileInputStream("myfile.txt");int b;b = in.read();while (b != -1){System.out.println((char) b);b = in.read();}}catch(IOException e){System.out.println(e.getMessage());}catch(FileNotFoundException e){e.printStackTrace();}finally {try{in.close();}catch(IOException e){e.printStackTrace();}}}
}
使用自定义的异常
使用自定义异常一般有如下步骤:
- 通过继承java.lang.Exception类声明自己的异常类
- 在方法适当的位置生成自定义异常的实例,并与throw语句抛出。
- 在方法的声明部分用throws语句声明该方法可能抛出的异常。
class MyException extends Exception{private int id;public MyException(String message,int id){super(message);this.id = id;}public int getId(){return id;}
}
public class Test {public void regist (int num) throws MyException {if(num<0) {throw new MyException ("人数为负值,不合理",3);}System.out.println("登记人数"+num);}public void manager(){try {regist(100);//100没有异常//regist(-1);} catch (MyException e){System.out.println("登记失败,出错类型码="+ e.getId());e.printStackTrace();}System.out.println("操作结束");}public static void main (String[] args){Test t =new Test();t.manager();}
}
regist(-1),输出为:
注意: 重写方法时,需要抛出与原方法所抛出异常类型一致的异常或不抛出异常