zoukankan      html  css  js  c++  java
  • Java异常的捕获与处理

    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("
    个性化异常信息:
    数组下标越界");
     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

    输出结果:

    setZero方法开始:
    setZero方法结束。
    异常:java.lang.ArrayIndexOutOfBoundsException: 10
    main()方法结束!

    四、RuntimeException类

    Exception和RuntimeException的区别:

    Exception:强制性要求用户必须处理;

    RunException:是Exception的子类,由用户选择是否进行处理。

                                   

    五、自定义异常类

    自定义异常类继承自Exception类,可以使用父类的大量的方法,也可自己编写方法来处理特定的事件。Java提供用继承的方式运行用户自己编写的异常类。

     1 class MyException extends Exception
     2 {
     3     public MyException(String message)
     4     {
     5         super(message);
     6     }
     7 }
     8 public class DefinedException
     9 {
    10     public static void main(String[] args) 
    11     {
    12         try
    13         {
    14             throw new MyException("
    自定义异常类!");
    15         }
    16         catch(MyException e)
    17         {
    18             System.out.println(e);
    19         }
    20     }
    21 }
  • 相关阅读:
    第一阶段意见评论
    《大道至简》阅读笔记03
    大二下第十二周学习笔记
    《大道至简》阅读笔记02
    课上练习—单词统计
    大二下第十一周学习笔记
    《大道至简》阅读笔记01
    《人月神话》阅读笔记03
    大二下第十周学习笔记
    团队项目前期冲刺-10
  • 原文地址:https://www.cnblogs.com/wxywxy/p/6759497.html
Copyright © 2011-2022 走看看