Java学习(十)

异常主要包括Error和Exception。前者主要是Java虚拟机运行过程中所产生的错误,后者为程序运行过程中所遇到的异常,具体有两大类:Runnable Exception和IOException两种。我们主要讨论的是Exception这种异常。

Exception类中有三种构造函数,分别为:1.public Exception(){};2.public Exception(String message){……};3.public Exception(String message, Throwable cause){……}。cause可以在低层具体描述抛出异常的原因,message表示抛出异常的信息。

比较常用的内部方法有:1.printStackTrace();2.getMessage();3.getCause()。方法一表示在出现异常时显示代码在栈中的位置,便于用户调试;方法二用于显示异常的信息;方法三用于显示抛出异常的底层信息。

异常的基本形式为try{……}catch(Exception1 e1){……}catch(Exception2 e2){……}finally{……};Exception1与Exception2具有优先级的关系。前者需要为子类,后者为父类。finally的表达式为接在catch表式后,无论catch中是否为break或者return,finally均会执行完成(除非不抛出异常)。

下面为具体例子:

 1 class ExceptionTest
 2 {
 3     private String msg;
 4     protected String str;
 5     public ExceptionTest(){}
 6     public ExceptionTest(String msg,String str)
 7     {
 8         this.msg=msg;
 9         this.str=str;
10     }
11     
12     public void display(int i)
13     {
14         try{
15             if(i==1)
16                 throw new NumberFormatException();
17             else if(i==2)
18                 throw new IOException();
19             str+=1;
20         }catch(NumberFormatException en)
21         {
22             str+=2;
23             return;
24         }catch(IOException ei)
25         {
26             str+=3;
27             return;
28         }
29         finally
30         {
31             str+=4;
32         }
33         str+=5;
34     }
35     
36     public void print()
37     {
38         System.out.println(str);
39         str="";
40     }
41 }
 1         ExceptionTest et=new ExceptionTest(){{str="";}};
 2         et.display(1);
 3         et.print();
 4         et.display(2);
 5         et.print();
 6         et.display(3);
 7         et.print();
 8 结果为:
 9 24
10 34
11 145

为保证程序的正常运行,一般会在方法或者类后接throws MyException以应对异常的抛出。自定义异常的基本思想为:用户自定义一个异常,继承于Exception或Exception的某个子类。也就是说系统定义的异常为父类,而用户定义的异常为子类。抛出异常时,首先显示的信息为子类的信息,然后层层链接,直到显示出底层的信息,这可以很方便的查找出问题。

以下为实例:NameException继承自Exception,SayHelloException继承自NameException。先判断名字是否正确,再判断年龄是否正确。如果都正确,则输出信息。

  1 import java.io.IOException;
  2 import java.util.*;
  3 
  4 class AndyException {
  5     private String name;
  6     private int age;
  7     private int n=0;
  8     String[] Name=new String[]{"Andy","Tom","Jane","Elizebath"};
  9     int[] Age=new int[]{24,23,25,22};
 10 
 11     public AndyException() {
 12     }
 13 
 14     public AndyException(String name, int age) {
 15         this.name = name;
 16         this.age = age;
 17     }
 18 
 19     public void sayHello() throws SayHelloException
 20     {
 21         int n=0;
 22         try
 23         {
 24             n=judgeName();
 25             judgeAge(n);
 26             
 27         }catch(SayHelloException she)
 28         {
 29             System.out.println(she.getMessage());
 30             return;
 31         }
 32         catch(NameException ne)
 33         {
 34             System.out.println(ne.getMessage());
 35             return;
 36         }
 37         System.out.println("Hello,"+Name[n]+" "+Age[n]);
 38     }
 39     
 40     
 41     public int judgeName() throws NameException {
 42         boolean flag = false;
 43         int n = 0;
 44         for (String temp : Name) {
 45             if (temp == this.name) {
 46                 flag = true;
 47                 break;
 48             }
 49             n++;
 50         }
 51         if (!flag) {
 52             throw new NameException("Name Error");
 53 
 54         } else {
 55             System.out.println("Name correct");
 56             return n;
 57         }
 58     }
 59     
 60     public void judgeAge(int n) throws SayHelloException
 61     {
 62         if (this.age != Age[n])
 63             throw new SayHelloException("Age Error");
 64         else
 65 
 66             System.out.println("Age correct");
 67     }
 68 
 69 }
 70 
 71 class SayHelloException extends NameException {
 72 
 73     private static final long serialVersionUID = 1L;
 74 
 75     public SayHelloException() {
 76     }
 77 
 78     public SayHelloException(String msg) {
 79         super(msg);
 80     }
 81 
 82     public SayHelloException(String msg, Exception cause) {
 83         super(msg, cause);
 84     }
 85 }
 86 
 87  class NameException extends Exception {
 88     
 89     private static final long serialVersionUID = 1L;
 90 
 91     public NameException() {
 92     }
 93 
 94     public NameException(String msg) {
 95         super(msg);
 96     }
 97 
 98     public NameException(String msg, Exception cause) {
 99         super(msg, cause);
100     }
101 }
 1                 AndyException ae=new AndyException("Andy",24);
 2         try
 3         {
 4         ae.sayHello();
 5         }catch(SayHelloException she)
 6         {
 7             System.out.println("Failed");
 8         }
 9 结果为:
10 Name correct
11 Age correct
12 Hello,Andy 24
原文地址:https://www.cnblogs.com/2Bthebest1/p/8422183.html