Java基础笔记-异常

Java中的异常机制:

Throwable类是 Java 语言中所有错误或异常的超类。主要包括两个子类: Error和Exception.

一般中要处理的异常是Exception.

Java中最常见的处理方式是try catch finally.格式为:

try

{

  需要被检测的异常.

}

catch(异常类 变量)

{

  处理异常的代码()处理方式

}

finally

{

  一定会被执行的代码语句

}

一个简单的异常处理代码及有关函数的应用:

 1 /*
 2 异常:Exception.
 3 处理异常的方式:
 4 try
 5 {
 6     需要被检测的代码.
 7 }
 8 catch(异常类 变量)
 9 {
10     处理异常的代码,处理方式.
11 
12 }
13 finally
14 {
15     一定会被执行的语句.
16 }
17 */
18 class ExceptionDemo
19 {
20     public static void main(String args [])
21     {
22         Demo d = new Demo();
23         //int a = d.div(4,1);
24         try
25         {
26             int a = d.div(4,0);
27             System.out.println("a = "+a);
28         }
29         catch(Exception e) //Exception类型的引用对象  Exception e = new ArithmeticException();
30         {
31             System.out.println("除以零了.");
32             System.out.println(e.getMessage()); //打印出异常信息. /by zero
33             System.out.println(e.toString()); // 异常名称 异常信息
34             
35             e.printStackTrace(); //JVM默认的就是调用printStackTrace方法获取异常处理,并打印出在堆栈中的跟踪信息.
36             
37         }
38         finally
39         {
40             System.out.println("It's over.");
41         }
42     }
43 }
44 
45 class Demo
46 {
47     int div(int a,int b)throws Exception //throws Exception 声明了该方法可能会出现问题.
48     {
49         return a/b;
50 }    }

用try catch finally也处理捕捉多个异常,在声明异常时,应该声明更具体的异常.

当出现了多个catch时,声明了多个异常时,父类异常块应该放在代码的最下面,如果放在最上面会导致子类异常永远无法执行到.

异常的声明:用关键字 throws.在java中可以用throws和throws来抛出异常.

throw和throws的区别在于:

throws:使用在函数上(所谓函数上就是在函数名的小括号与大括号之间),后面跟异常类,可以有多个异常类,用","(逗号)隔开即可.

throw:使用在函数内(在大括号之内),后面跟的是异常对象,格式例如: throw new 异常类名();

在实际应用中,可以自定义异常,自定义异常需要继承一个异常类,可以是Error类,Exception类,或者它们的根类:Throwable类.

格式:(例如声明一个自定义的Exception异常类)

class  自定义异常类名(一般写成xxxException) extends Exception

{

  xxxException(String message)

  {

    super(message);

  }

}

上面的自定义异常类,因为java已经在父类中已经把异常类的信息都完成了,子类异常类在构造时,

将异常的信息传递给父类,通过super语句,就可以直接调用getMessage()获取异常信息.(有关父类的方法可以查阅Java API)

自定义异常类只能手动抛出,是无法自动抛出的.

相关的例子代码:

 1 class ExceptionDemo2
 2 {
 3     public static void main(String args [])
 4     {
 5         Demo d = new Demo();
 6         try
 7         {
 8             int a = d.div(4,-9);
 9             System.out.println("a = "+a);
10         }
11         catch(FuShuException e)    
12         {
13             System.out.println(e.toString());
14             //System.out.println("出现了负数.");
15             System.out.println("错误的负数是:"+e.getValue());
16         }        
17         System.out.println("It's over.");
18         
19     }
20 }
21 
22 class FuShuException extends Exception
23 {
24     private int value;
25     /*
26     private String msg;
27     FuShuException(String msg)
28     {
29         this.msg = msg;
30     }
31     public String getMessage()
32     {
33         return msg;
34     }
35     */
36     FuShuException(String msg,int value)
37     {
38         super(msg);
39         this.value = value;
40     }
41     
42     public int getValue()
43     {
44         return value;
45     }
46 }
47 
48 class Demo
49 {
50     int div(int a,int b)throws FuShuException  //throws 使用在函数上.
51     {
52         if(b<0)
53             throw new FuShuException("出现了除数是负数的情况",b); //throw 使用在函数内.
54         return a/b;
55 }    }

Java中有一种特别的异常类:RuntimeException

1.该异常如果在函数内抛出了,在函数上不声明,编译也是可以通过的,(其他Exception等类是在函数内抛出了必须在函数上声明的)

2.如果在函数上声明了该类异常,调用者可以不进行处理.

因此使用自定义异常的时候,如果该异常的发生,无法继续进行运算,那么就可以让该自定义异常继承RuntimeException.

最后是一个异常的相关练习代码:

 1 class BlueScreen extends Exception
 2 {
 3     BlueScreen(String message)
 4     {
 5         super(message);
 6     }
 7 }
 8 class Smoke extends Exception
 9 {
10     Smoke(String message)
11     {
12         super(message);
13     }
14 }
15 
16 class NoPlanException extends Exception
17 {
18     NoPlanException(String message)
19     {
20         super(message);
21     }
22 }
23 
24 class Computer
25 {
26     private int state = 3;
27     
28     public void run()throws BlueScreen,Smoke
29     {
30         if(state==2)
31             throw new BlueScreen("电脑蓝屏了");
32         if(state==3)
33             throw new Smoke("电脑冒烟了");
34         System.out.println("电脑运行");
35     }
36     public void reset()
37     {
38         state = 1;
39         System.out.println("电脑重启");
40     }    
41 }
42 
43 class Teacher
44 {
45     private String name;
46     private Computer comp;
47     Teacher(String name)
48     {
49         this.name = name;
50         comp = new Computer();
51     }
52     public void prelect()throws NoPlanException
53     {
54         try
55         {
56             comp.run();
57         }
58         catch(BlueScreen e)
59         {
60             test();
61             comp.reset();
62         }
63         catch(Smoke e)
64         {
65             throw new NoPlanException("课时无法继续."+e.getMessage());
66         }
67         System.out.println("老师讲课");
68     }
69     public void test()
70     {
71         System.out.println("练习");
72     }
73 }
74 
75 
76 class ExceptionTest
77 {
78     public static void main(String args[])
79     {
80         Teacher t = new Teacher("张老师");
81         try
82         {
83             t.prelect();
84         }
85         catch(NoPlanException e)
86         {
87             System.out.println(e.toString());
88             System.out.println("换老师或者电脑,不行就放假.");
89         }
90     }
91 }

  

原文地址:https://www.cnblogs.com/jerryho/p/4518456.html