Java提供了try(尝试)、catch(捕捉)、finally(最终)这三个关键字来处理异常。在处理各种异常时,需要用到对应的异常类,指的是由程序抛出的对象所属的类。

一、异常处理的使用

由于finally块是可以省略的,异常处理格式可以分为三类:try{ }——catch{ }、try{ }——catch{ }——finally{ }、try{ }——finally{ }。

 1 public class DealException
 2 {
 3     public static void main(String args[])
 4     {    
 5         try
 6         //要检查的程序语句
 7         {
 8             int a[] = new int[5];
 9             a[10] = 7;//出现异常
10         }
11         catch(ArrayIndexOutOfBoundsException ex)
12         //异常发生时的处理语句
13         {
14             System.out.println("超出数组范围!");
15         }
16         finally
17         //这个代码块一定会被执行
18         {
19             System.out.println("*****");
20         }
21         System.out.println("异常处理结束!");
22     }
23 }

可以看出,在异常捕捉的过程中要进行两个判断,第一是try程序块是否有异常产生,第二是产生的异常是否和catch()括号内想要捕捉的异常相同。

那么,如果出现的异常和catch()内想要捕捉的异常不相同时怎么办呢?事实上我们可以在一个try语句后跟上多个异常处理catch语句,来处理多种不同类型的异常。

 1 public class DealException
 2 {
 3     public static void main(String args[])
 4     {    
 5         try
 6         //要检查的程序语句
 7         {
 8             int a[] = new int[5];
 9             a[0] = 3;
10             a[1] = 1;
11             //a[1] = 0;//除数为0异常
12             //a[10] = 7;//数组下标越界异常
13             int result = a[0]/a[1];
14             System.out.println(result);
15         }
16         catch(ArrayIndexOutOfBoundsException ex)
17         //异常发生时的处理语句
18         {
19             System.out.println("数组越界异常");
20             ex.printStackTrace();//显示异常的堆栈跟踪信息
21         }
22         catch(ArithmeticException ex)
23         {
24             System.out.println("算术运算异常");
25             ex.printStackTrace();
26         }
27         finally
28         //这个代码块一定会被执行
29         {
30             System.out.println("finally语句不论是否有异常都会被执行。");
31         }
32         System.out.println("异常处理结束!");
33     }
34 }

上述例子中ex.printStackTrace();就是对异常类对象ex的使用,输出了详细的异常堆栈跟踪信息,包括异常的类型,异常发生在哪个包、哪个类、哪个方法以及异常发生的行号。

二、throws关键字

throws声明的方法表示该方法不处理异常,而由系统自动将所捕获的异常信息抛给上级调用方法。

 1 public class throwsDemo
 2 {
 3     public static void main(String[] args) 
 4     {
 5         int[] a = new int[5];
 6         try
 7         {
 8             setZero(a,10);
 9         }
10         catch(ArrayIndexOutOfBoundsException ex)
11         {
12             System.out.println("数组越界错误!");
13             System.out.println("异常:"+ex);
14         }
15         System.out.println("main()方法结束。");
16     }
17     private static void setZero(int[] a,int index) throws ArrayIndexOutOfBoundsException
18     {
19         a[index] = 0;
20     }
21 }

 throws关键字抛出异常,“ArrayIndexOutOfBoundsException”表明setZero()方法可能存在的异常类型,一旦方法出现异常,setZero()方法自己并不处理,而是将异常提交给它的上级调用者main()方法。

三、throw关键字

throw的作用是手工抛出异常类的实例化对象。

 1 public class throwDemo
 2 {
 3     public static void main(String[] args) 
 4     {
 5         try
 6         {
 7             //抛出异常的实例化对象
 8             throw new ArrayIndexOutOfBoundsException("\n个性化异常信息:\n数组下标越界");
 9         }
10         catch(ArrayIndexOutOfBoundsException ex)
11         {
12             System.out.println(ex);
13         }
14     }
15 }

我们能发现,throw好像属于没事找事,引发运行期异常,并自定义提示信息。事实上,throw通常和throws联合使用,抛出的是程序中已经产生的异常类实例。

 1 public class ExceptionDemo
 2 {
 3     public static void main(String[] args) 
 4     {
 5         int[] a = new int[5];
 6         try
 7         {
 8             setZero(a,10);
 9         }
10         catch(ArrayIndexOutOfBoundsException e)
11         {
12             System.out.println("异常:"+e);
13         }
14         System.out.println("main()方法结束!");
15     }
16     public static void setZero(int[] a,int index) throws ArrayIndexOutOfBoundsException
17     {
18         System.out.println("setZero方法开始:");
19         try
20         {
21             a[index] = 0;
22         }
23         catch(ArrayIndexOutOfBoundsException ex)
24         {
25             throw ex;
26         }
27         finally
28         {
29             System.out.println("setZero方法结束。");
30         }
31     }
32 }
ExceptionDemo

分类:

技术点:

相关文章: