java异常处理

异常处理

 1 /**
 2  *异常: 就是程序在运行时出现不正常情况
 3  *异常由来: 问题也是现实生活中的事物,也可以通过java类的形式进行描述,并进行封装. 
 4  *
 5  *对于问题的划分:  
 6  *   1: 严重的问题 (Error) :   java通过Error类进行描述,一般不编写针对性的代码进行处理
 7  *   2: 非严重的问题 (Exception): java通过Exception类进行描述,一般使用针对性的处理方式进行处理
 8  *   
 9  *无论 Error 或者 Exception 都具有一些共性的内容
10  *比如: 不正常的情况信息,引发原因等  
11  *  所以就出现了 "Throwable" 这个父类
12  *  
13  * Throwable
14  *    |--Error
15  *    |--Exception 
16  *  
17  *
18  *2.异常的处理
19  *  java 提供了特有的语句进行处理
20  *  
21  * try{
22  *     需要被检测
23  * }catch(异常类 变量){
24  *    出来异常的代码:(处理方式)
25  * }finally{
26  *   一定会执行的语句
27  * }
28  * 
29  * 3.对捕获到的异常信息进行处理
30  *   1. String getMessage();        //打印  异常信息
31  *   2. String toString();          //打印  异常名称 : 异常信息
32  *   3. void printStackTrace();   //打印最全名:  异常 名称 : 异常信息  : 异常位置  ; 是 JVM 默认异常处理机制,就是调用这个方法
33  * 
34  * 4.问题: 发现下面的程序有时候能正常运行,有时候不能, 因为不是所以认得知道这个方法会抛出异常 ,也不是所有人都会处理 这个异常. 怎么办 ?
35  *   所以就出现  throws  抛出异常
36  */
37 class Demo{
38     static int div(int a,int b){
39         return a/b;
40     }
41 }
42 
43 
44 public class ExceptionDemo {
45     public static void main(String[] args) {
46         //exception
47         try{
48             int x=Demo.div(4, 0); //除数不能为0 ,抛出 new ArithmeticException(); 程序会终止 
49             System.out.println("x="+x);  //出现异常后  这句话就不会被执行  直接进入 catch 语句块中去了
50         }catch(Exception e){  // Exceptioin e= new ArithmeticException();
51             System.out.println("除数为0啦!");
52             System.out.println(e.getMessage()); // "/ by zero"
53             System.out.println(e.toString());//toString() 可以省略   打印   异常名称 : 异常信息
54             e.printStackTrace();   //打印最全名:  异常 名称 : 异常信息  : 异常位置.
55             
56         }
57         System.out.println("run over"); //运行 完  catch语句块后 , 接着运行 这句
58         
59         //error
60         byte[] arr=new byte[1024*1024*6000];
61         //超出虚拟据总内存大小  不管在怎么样 也不能超过物理内存大小 ,这程序也没戏了.
62     }
63 }

throws  多异常处理

 1 /**
 2  * throws
 3  * 1. throws: 在功能上通过throws的关键字声明该功能有可能出现问题.
 4  * 
 5  * 2.被声明后  我们必须告诉程序的处理方式
 6  *    1.采用 try catch 进行处理
 7  *    2.继续抛出 异常 ,由jVM 虚拟机 进行异常处理  不过这样 程序 就会终止.
 8  *    
 9  *    
10  * 多异常的处理
11  *   1.声明异常时,建议声明更为具体的异常,这样 处理的可以更具体 
12  *     比如: 下面 throws ArithmeticException 而不写成 throws Exception
13  *     
14  *   2.对于声明多个异常,在调用的时候就应该有几个 catch块.不要定义多余的catch块 ,比如下面注释的 catch块
15  *      如果多个catch中的异常出现了继承关系,父类异常catch块 放在最下面 .
16  *      
17  *   建议:在进行catch处理时,catch一定要定义具体的处理方式
18  *       不要简单的一句. e.printStackTrace();
19  *       也不要简单的就书写一条输出语句 ,一般采用log日志文件 进行 异常信息记录
20  *
21  */
22 
23 class Demo{
24     static int div(int a,int b) throws ArithmeticException{
25         
26         return a/b;
27     }
28 }
29 
30 class Demo1{
31     static int div(int a,int b) throws ArithmeticException,ArrayIndexOutOfBoundsException{
32         int [] arr=new int[a];
33         System.out.println(arr[a+1]);  //出现越界
34         return a/b;
35     }
36 }
37 
38 public class ExceptionDemo1 {
39 
40     /**
41      * thrwos
42      */
43     public static void main(String[] args) {
44         // 使用第一种方法处理
45         try {
46             Demo.div(4, 0);
47         } catch (Exception e) {
48             System.out.println(e.toString());
49         }
50     }
51     
52     // 使用第二种方法处理  继续往上抛出异常 让 JVM 帮我处理  
53     public static void otherMethod() throws ArithmeticException{
54         Demo.div(4, 0);
55     }
56 
57     /**
58      * 多异常处理
59      */
60     public static void otherMethod1() throws ArithmeticException,ArrayIndexOutOfBoundsException{
61         Demo1.div(4, 0);
62     }
63     
64     public static void otherMethod2() {
65         try {
66             Demo1.div(4, 0);
67         } catch (ArithmeticException e) {
68             System.out.println(e.toString());
69             System.out.println("被0除了");
70         }catch(ArrayIndexOutOfBoundsException e){
71             System.out.println(e.toString());
72             System.out.println("角标越界");
73         }/*catch(Exception e){  //这样做只会隐藏 异常 ,这样做是不对的
74             System.out.println(e.toString());
75         }*/
76          
77     }
78     
79 }

throws 和throw 的区别    
自定义 异常类

 1 /**
 2  * 因为项目中会出现特有的问他你.
 3  * 而这些问题并未被java所描述和封装成对象
 4  * 所以对于一些特有的问题可以安装java的对问题封装的思想.
 5  * 将特有的问题,进行自定义的异常封装
 6  * 
 7  * 
 8  * 自定义异常
 9  * 
10  * 需求: 在本程序中,对于除数 是-1 也视为错误的,是无法进行运算.
11  * 那么就需要对这个问题进行自定义的描述.
12  *
13  *当函数内部初相了throw抛出异常对象,那么就必须要给对应的处理动作.
14  *要么在内部 try catch 处理
15  *要么子啊函数上声明让调用者处理.
16  *
17  *一般情况下, 函数内部出现异常,函数上需要声明.
18  *
19  *发现 打印的结果中只有异常的名称,却没有异常的信息
20  *因为自定义的异常并未定义信息
21  *
22  *如何定义异常信息呢?
23  *  因为父类中已经把异常信息的操作都做完了
24  *  所以子类只要在构造时,将异常信息传递给父类通过super语句
25  *  就可以直接通过getMessage获取自定义的异常信息
26  *
27  *自定义异常:必须是自定义类继承 Exception
28  *
29  *继承Exception原因:
30  *  异常体系有一个特点,因为异常类和异常对象都被抛出
31  *  因为他们都具备可抛性,这个可抛性是Trowable这个体系中独有的特点.
32  *
33  *  只有这个体系中的类和对象才可以被throw 和 throw 操作
34  *  
35  *throws 和throw 的区别
36  *throws:使用在函数上
37  *throw:使用在函数内
38  *
39  *throws:后面跟的异常类,可以跟多个,用逗号分开
40  *throw:后跟的是异常对象.
41  */
42 class FuShuException extends Exception{
43     
44     /*
45     private String msg;
46     FuShuException(String msg){
47         this.msg=msg;
48     }
49     public String getMessage(){
50         return this.msg;
51     }
52     */
53     FuShuException(String msg,int num){
54         super(msg);
55         this.value=num;
56     }
57     private int value;
58     
59     public int getValue(){
60         return value;
61     }
62     
63 }
64 
65 //自定义 异常类
66 class Demo{
67     
68     static int div(int a,int b) throws FuShuException{
69         if(b<0)
70             throw new FuShuException("出现了除数为零的情况  / by fushu",b);  //手动通过throw 关键字抛出一个自定义异常对象.
71         return a/b;
72     }
73 }
74 
75 
76 public class ExceptionDemo2 {
77 
78     public static void main(String[] args) {
79         // TODO Auto-generated method stub
80         try {
81             Demo.div(4, -1);
82         } catch (FuShuException e) {
83             System.out.println(e.toString());
84             System.out.println("除数出现负数了!");
85             System.out.println("负数是:"+e.getValue());
86         }
87         System.out.println("over");
88     }
89 
90 }

RuntimeException

  1 /**
  2  *RuntimeException:
  3  *  Exception中有一个非常特殊子类异常,RuntimeException 运行时异常
  4  *  如果在函数内部使用throw抛出异常,函数上可以不用throws声明,编译一样通过.
  5  *  如果在函数上使用throws声明了该异常,调用者可以不用进行处理,编译一样通过;
  6  *之所以不用在函数上使用throws声明,是因为不需要让调用者处理
  7  *当异常发送,JVM希望程序停止,因为在运行时,出现了无法继续运算的情况,希望停止程序后,然程序员对代码进行修正
  8  *
  9  *自定义异常时,如果该异常的发生,无法在继续进行运算,就让自定义异常继承RuntimeException.
 10  *
 11  *对于异常的分类
 12  *1.编译时被检测的异常 (必须使用 throws 或者 try catch进行处理)
 13  *
 14  *2.编译时不被检测的异常(运行时异常,RuntimeException以及其子类) 程序会停止掉,然后要改进代码
 15  *
 16  */
 17 class Demo{
 18     static int div(int a,int b){ //发现  这里不需要 throws 怎么回事?
 19         if(b==0)
 20             throw new ArithmeticException("被零除了!"); //需改 java自定义异常中的提示,改成我们熟悉的
 21         return a/b;
 22     }
 23     
 24     static int div1(int a,int b)throws ArithmeticException{ 
 25         //if(b==0)
 26             //throw new Exception("被零除了!"); //需改 java自定义异常中的提示
 27         return a/b;
 28     }
 29     
 30     static int div2(int a,int b){ //发现  这里不需要 throws 怎么回事?
 31         if(b<0)
 32             throw new FuShuException("出现除数为负数了"); //需改 java自定义异常中的提示
 33         if(b==0)
 34             throw new ArithmeticException("被零除了!");
 35         return a/b;
 36     }
 37     
 38 }
 39 //自定义 运行时异常类
 40 class FuShuException extends RuntimeException{
 41     
 42     public FuShuException(String msg) {
 43         super(msg);
 44     }
 45 }
 46 
 47 public class ExceptionDemo3 {
 48 
 49     public static void main(String[] args) {
 50         // TODO Auto-generated method stub
 51         /*Demo.div(4, 0);   // 
 52         Demo.div1(4, 0);  //发现没有 try catch  编译也通过
 53         
 54         Demo.div2(4, -9);*/
 55         
 56         
 57         //讲课
 58         Teacher t=new Teacher("张老师");
 59         try {
 60             t.prelect();
 61         } catch (NoPlanException e) {
 62             System.out.println(e.toString());
 63             System.out.println("换老师 或者 放假!");
 64         }
 65     }
 66 
 67 }
 68 
 69 
 70 /*
 71  * 老师上课
 72  * 
 73  * 思考上课中出现的问题 
 74  *   比如问题: 电脑蓝屏
 75  *           电脑冒烟
 76  *           
 77  *  要对问题进行封装成对象.
 78  *  冒烟后,出现讲课进度无法继续
 79  *  出现了讲师的问题,课时计划无法完成.
 80  *  
 81  *  throw 和 return 单独存在的时候,后面不能出现 其他语句 
 82  */
 83 class Teacher{
 84     private String name;
 85     
 86     private Computer cmpt;
 87     Teacher(String name){
 88         this.name=name;
 89         this.cmpt=new Computer();
 90     }
 91     
 92     public void prelect()throws NoPlanException{
 93         try {
 94             cmpt.run();
 95         } catch (LanPingException e) {
 96             cmpt.reset();
 97         } catch (MaoYanException e) {
 98             test();
 99             throw new NoPlanException("课时无法继续"+e.getMessage());
100             //test(); //不能放在这里 ,throw 和 return 单独存在的时候,后面不能出现 其他语句 
101         }
102         System.out.println("讲课");
103     }
104     
105     public void test(){
106         System.out.println("练习!");
107     }
108     
109     
110 }
111 class Computer{
112     private int status=3;
113     public void run()throws LanPingException,MaoYanException {
114         if(status==2)
115             throw new LanPingException("蓝屏了");
116         if(status==3)
117             throw new MaoYanException("冒烟了");
118         
119         System.out.println("电脑运行");
120     }
121     public void reset(){
122         this.status=1;
123         System.out.println("电脑重启");
124     }
125 }
126 
127 //封装问题
128 class LanPingException extends Exception{
129     LanPingException(String message){
130         super(message);
131     }
132 }
133 
134 class MaoYanException extends Exception{
135     MaoYanException(String message){
136         super(message);
137     }
138 }
139 //冒烟后的异常
140 class NoPlanException extends Exception{
141     NoPlanException(String message){
142         super(message);
143     }
144 }

finally

 1 /**
 2  * finally代码快:定义一定要执行的代码
 3  * 通常用于关闭资源
 4  * 如果 catch中 有return 的时候  finally 代码会被执行 而其他的代码不会被执行.
 5  */
 6 class FuShuException extends Exception{
 7     FuShuException(String msg){
 8         super(msg);
 9     }
10 }
11 
12 class Demo{
13     static int div(int a,int b)throws FuShuException{
14         if(b<0)
15             throw new FuShuException("除数为负数!");
16         return a/b;
17     }
18 }
19 class NoException extends Exception{
20     
21 }
22 public class FinallyDemo1 {
23 
24     public static void main(String[] args) {
25         test1();  //   除数为负数!     finally   over
26         
27         test2(); //   除数为负数!      finally
28     }
29     public static void test1(){
30         try {
31             int x=Demo.div(4, -1);                       //1
32             System.out.println("x="+x);
33         } catch (FuShuException e) {
34             System.out.println(e.toString());            //2
35         }finally{
36             System.out.println("finally");               //3
37         }
38         System.out.println("over");                      //4
39     }
40     
41     public static void test2(){
42         try {
43             int x=Demo.div(4, -1);                       //1
44             System.out.println("x="+x); 
45         } catch (FuShuException e) {
46             System.out.println(e.toString());            //2  
47             return; // 使用return后                                                                 //4
48         }finally{
49             System.out.println("finally");               //3
50         }
51         System.out.println("over");
52     }
53     
54     
55     /**
56      * 采用分层的思想来设计 一般不会抛出 SQLException 给调用者 而是给一个 调用者能看的懂的异常
57      */
58     public void method()throws /*SQLException*/NoException {
59         //连接数据库
60         //数据操作:  //throw new SQLException();
61         //关闭数据库
62         
63         try {
64             //连接数据库
65             //数据操作: 
66         }catch (SQLException e) {
67             //会对数据库进行异常操作
68             //throw new SQLException();
69             throw new NoException();
70         }finally{
71             //关闭数据库
72         }
73     }
74 
75 }
 1 /**
 2  * 
 3  *异常的几种格式
 4  *
 5  *记住一点:catch是用户处理异常,如果没有catch就代表异常没有被处理过,如果该异常是检测时异常,那么使用 throws必须声明.
 6  */
 7 class ExceptionStyle{
 8     //异常的几种格式
 9     public void method(){
10         //第一种格式
11         try {
12             
13         } catch (Exception e) {
14             // TODO: handle exception
15         }
16         
17         //第二种格式
18         try {
19             
20         } catch (Exception e) {
21             // TODO: handle exception
22         }finally{
23             
24         }
25         //第三格式
26         try{
27             
28         }finally{
29             
30         }
31         
32         //第四种 格式
33         try {
34             
35         }catch (ArithmeticException e) {
36             // TODO: handle exception
37         }catch (Exception e) {
38             // TODO: handle exception
39         }
40         
41     }
42     
43     //看看下面几种情况 
44     
45     //错误       没有throws  Exception  编译不通过
46     public void method1() /*throws  Exception*/{
47         throw new Exception();   
48     }
49     
50     //正确
51     public void method2(){
52         try {
53             throw new Exception();
54         } catch (Exception e) {
55             // TODO: handle exception
56         }
57         
58     }
59     
60     //错误           处理一个异常后  又抛出了异常  编译不通过
61     public void method3(){
62         try {
63             throw new Exception();
64         } catch (Exception e) {
65             throw e;
66         }
67         
68     }
69     
70     //正确         只要  e 被处理就可以了
71     public void method4(){
72         try {
73             throw new Exception();
74         } catch (Exception e) {
75             try {
76                 throw e;
77             } catch (Exception e2) {
78                 // TODO: handle exception
79             } 
80         }
81         
82     }
83     
84     //错误      没有catch 或则 throws 就是没有处理  编译 不通过 ,
85     public void method5(){
86         try {
87             throw new Exception();
88         }finally{
89             //关闭资源   产生的异常 有调用者处理
90         }
91     }
92     
93 }

 

posted @ 2013-05-08 14:22  vincent_hv  阅读(260)  评论(0编辑  收藏  举报