当前位置:文档之家› Java中异常处理

Java中异常处理

第13章异常课堂练习(2理论+2习题课+2上机)【教学目标】➢了解异常和异常处理的概貌(第13.2节)。

➢探究使用异常处理的优点(第13.3节)。

➢区别异常的类型:Error (致命的) 和Exception (非致命的), 以及必检和免检异常(第13.4节)。

➢在方法头中声明异常(第13.5.1节)。

➢在方法中抛出异常(第13.5.2节)。

➢编写try-catch 块处理异常(第13.5.3节)。

➢解释异常是如何传播的(第13.5.3节)。

➢在try-catch块中使用finally子句(第13.6节)。

➢只为非预期错误使用异常(第13.7节)。

➢在catch 块中重新抛出异常(第13.8节)。

➢创建链式异常(第13.9节)。

➢定义自定制的异常类(第13.10节)。

【教学重点】➢了解异常的处理机制➢异常的解决方法➢Exception类的作用及其使用方法➢必检异常、免检异常的特点【基本知识点】1、常见异常演示(1)算数异常:ng.ArithmeticException在算术运算中,0作为除数时,系统将产生该异常,示例:public static void main(String[] args) {int x=10;int y=0;int m=x/y;}Exception in thread "main" ng.ArithmeticException: / by zero at exception.NotChecketException.main(NotChecketException.java:7)(2)数组越界异常:ng.ArrayInd exOutOfBoundsException在数组下标越界时,将产生该异常,示例:public static void main(String[] args) {int arr[]={1,2,3};System.out.println(arr[3]);}Exception in thread "main" ng.ArrayIndexOutOfBoundsException: 3 at exception.NotChecketException.main(NotChecketException.java:6)(3)数组存储异常:ng.ArrayStoreException在数组中存入与之声明类型不符的类型,将产生该异常,示例:public static void main(String[] args) {boolean[] b = {false,true,false};int attr[] = {1,2,3};System.arraycopy(attr, 0, b, 0, 1);}Exception in thread "main" ng.ArrayStoreException at ng.System.arraycopy(Native Method)at exception.NotChecketException.main(NotChecketException.java:7)(4)对象转换异常:ng.ClassCastException将对象A转换成为对象B时,如果A既不是与B同一个类,也不是B的子类,将产生该异常,示例:public static void main(String[] args) {Object obj = new Object();int in[] = (int[]) obj;}Exception in thread "main" ng.ClassCastException:ng.Object cannot be cast to [Iat exception.NotChecketException.main(NotChecketException.java:6)(5)空指针异常:ng.NullPointerException引用空对象的实例或方法时,将产生该异常。

示例:public static void main(String[] args) {String str = null;System.out.println(str.length());}Exception in thread "main" ng.NullPointerExceptionat exception.NotChecketException.main(NotChecketException.java:6) 2、try…catch语句捕获异常public class ExceptionDemo1 {public static void main(String[] args) {int i[] = { 1, 2, 3 };try {System.out.println("i[0]=" + i[0]);System.out.println("i[3]=" + i[3]);} catch (ArrayIndexOutOfBoundsException e) {System.out.println("出现异常" + e.getMessage());}System.out.println("i[1]=" + i[1]);}}以上面的程序运行结果可以发现,在程序中加入异常处理代码后,当异常发生时,整个程序没有因为异常的出现而中断执行。

3、try…catch…finally语句捕获异常public class ExceptionDemo4 {public static void main(String[] args) {int i[] = { 1, 2, 3 };try {System.out.println("i[0]=" + i[0]);System.out.println("i[3]=" + i[3]);} catch (ArrayIndexOutOfBoundsException e) {System.out.println("出现异常" + e.getMessage());}finally{System.out.println("......无论是否有异常,都会执行finally语句......");}System.out.println("i[1]=" + i[1]);}}其中,不论try代码块中发生了哪种异常,也不论try代码块和catch代码块是否可以正常执行,finally代码块都保证会执行。

即使前面的try…catch代码块都无法捕获这个异常,或者在catch代码块中还有其他异常产生,在将新的异常传递给java运行时环境之前都会先执行finally代码块。

4、多个catch代码块public class ExceptionDemo5 {public static void main(String[] args) {FileReader fr = null;try {fr=new FileReader("test.txt");fr.read();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{try {fr.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}使用多个catch语句块的时候,请注意catch代码块的排顺序问题。

Catch代码块捕获异常是按照顺序的,如果前面已经有一个异常类型被捕获了,但又同时满足后面的异常类型时,就不会被捕获了。

5、在调用方法出处理被抛出的异常public class Exception6 {public static void arrException() throws IOException{ FileReader fr = new FileReader("不存在的文档.txt");fr.close();}public static void main(String[] args) {try {arrException();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}在上面的程序中,“不存在的文档”表示在同一个目录下没有该文档。

当arrException()方法产生了异常,此方法并不作处理,而是交由调用arrException()方法的main方法处理。

6、错误的使用throws在上面的程序中,arrException()方法抛出的异常类型为Exception。

比调用arrException()方法的main方法能处理的异常范围大,所以编译错误。

7、主方法main使用throws关键字public class ExceptionDemo8 {public static void arrException() throws Exception{ FileReader fr = new FileReader("不存在的文档.txt");fr.close();}public static void main(String[] args) throws Exception { arrException();}}8、若需要在程序中自行抛出异常,可以使用throw关键字。

Throw语句可以单独使用,用于方法体内部,throw抛出的是异常类的实例。

如果抛出了检查异常,则还应该在方法头部声明方法可能抛出的异常类型,该方法的调用者也必须检查处理抛出的异常。

如果抛出的而是Error或RuntimeException,则该方法的调用者可选择是否处理该异常。

示例:自行抛出免检异常public class ExceptionDemo9 {public static void arrException() {throw new NullPointerException("自行抛出的免检异常--空指针异常");}public static void main(String[] args) {try {arrException();} catch (NullPointerException e) {e.printStackTrace();}}}自行抛出检查异常public class ExceptionDemo10 {public static void arrException() throws IOException { throw new IOException("自行抛出的免检异常--IO异常");}public static void main(String[] args) {try {arrException();} catch (IOException e) {e.printStackTrace();}}}上述两个程序产生的异常对象都是自行抛出的,它们的区别是,使用throw关键字抛出免检异常时,不需要在方法头部声明方法可能抛出的异常类型。

相关主题