首页 > 其他 > 详细

第09章 异常

时间:2015-08-28 17:02:45      阅读:126      评论:0      收藏:0      [点我收藏+]

  1 /*****************
  2 ***第09章 异常
  3 *******知识点:
  4 **************1.异常的基础知识
  5 **************2.异常类类图
  6 **************3.异常关键字  
  7 **************4.异常处理机制 
  8 **************5.异常继承
  9 **************6.异常小结
 10 */
 11 
 12 /*
 13 *演示用的自定义异常类
 14 */
 15 class MyException extends Exception{
 16     private String msg;
 17     public MyException(String msg){
 18         super();
 19         this.msg = msg;
 20     }
 21     public String toString(){
 22         return ("异常信息:" + msg);
 23     }
 24 }
 25 
 26 public class test9{
 27     public static void main(String[] args){
 28         showDemo("1.异常的基础知识");
 29         demoExceptionBasicKnowledge();//演示1.异常的基础知识
 30         
 31         showDemo("2.异常类类图");
 32         demoExceptionClassView();//演示2.异常类类图
 33         
 34         showDemo("3.异常关键字");
 35         demoExceptionKeyWord();//演示3.异常关键字
 36         
 37         showDemo("4.异常处理机制");
 38         demoExceptionHandle();//演示4.异常处理机制
 39         
 40         showDemo("5.异常继承");
 41         demoExceptionExtend();//演示5.异常继承
 42         
 43         showDemo("6.异常小结");
 44         demoExceptionNote();//演示6.异常小结
 45     }
 46     
 47     /*
 48     *1.异常的基础知识
 49     */
 50     public static void demoExceptionBasicKnowledge(){
 51         //异常————非正常情况,出现这样错误其实是程序上的错误。其中包括逻辑错误和系统错误
 52         //比如:数组下标越界、除数为0、内存溢出等
 53     }
 54     
 55     /*
 56     *2.异常类类图
 57     */
 58     public static void demoExceptionClassView(){
 59         //异常类的根类————java.lang.Throwable  其中有两个子类分别为:Error、Exception
 60         // 1.Error类:无法处理的异常,也成为错误。如:操作系统错误、内存硬件损坏等(编程时无需关心这类异常)
 61         // 2.Exception类:可处理的异常。其中有两个子类分别为:RuntimeException和非RuntimeException
 62         //                                (有时也称为unchecked exception——非检查异常(RuntimeException) 和checked exception——检查异常)
 63         //        2.1RuntimeException:运行时异常或非检查异常。对于运行时异常,不要求必须捕获处理或抛出异常,由程序员自行决定
 64         //        2.2非RuntimeException:非运行时异常或检查异常。对于非运行时异常,要求必须捕获处理或抛出异常,如不捕获或抛出则编译不通过
 65     }
 66     
 67     /*
 68     *3.异常关键字  
 69     */
 70     public static void demoExceptionKeyWord(){
 71         //关键字有:try catch finally throw throws
 72         //1.try catch finally关键字
 73         //    1.1 try关键字    包围可能出现异常的逻辑代码,不能单独使用。必须配合catch/finally一同使用
 74                 //如:  try..catch..;    try..catch..finally..;  try..finally;  只能有一个try  可以有多个catch  finally可选
 75         //    1.2 catch关键字   用于捕获可能出现的异常  建议不要使用空catch块
 76         //  1.3    finally关键字   不管有没捕获到异常,finally代码均会执行,常用于释放资源操作
 77         //  三者的执行顺序为:try—>catch—>finally  但如果存在多个catch,一旦捕获到异常,后面的catch则不会执行,
 78         //  且finally在return前执行   建议不要在finally中使用return  不然返回值会被覆盖掉
 79         showDemo("演示try..catch");
 80         testTryCatch();
 81         
 82         showDemo("演示try..catch..finally");
 83         testTryCatchFinaly();
 84         //2.throw throws
 85         //    2.1 throw关键字  抛出一个异常对象  只会出现在方法体内
 86         //    2.2 throws关键字  出现在方法的声明中,表示可能会抛出的异常,可允许后面跟着多个异常类型
 87         showDemo("演示throws");
 88         try{
 89             testThrows();//演示throws
 90         }catch(ArithmeticException e){
 91             System.out.println("除数都能为0?哥笑了!");
 92         }
 93         
 94         showDemo("演示throw");
 95         try{
 96             testThrow();//演示throw
 97         }catch(ArithmeticException e){
 98             System.out.println("除数都能为0?大哥我笑了!");
 99         }
100     }
101     
102     /*
103     *4.异常处理机制
104     */
105     public static void demoExceptionHandle(){
106         //1.对于可能出现异常的代码,处理方式有两种
107         //    1.1处理异常————使用try..catch语句对异常进行处理
108         //        如:  try{ ...} catch(异常类型1){} catch(异常类型2){} catch(异常类型3){} finally{}
109         //    1.2不处理异常————对于处理不了的异常或者需要转型的异常在方法声明处使用throws语句进行异常的抛出
110         //        如:public void testException() throws Exception{ if(...) { throw new Exception();}}
111         
112         //2.使用异常类的方式有两种:
113         //    2.1使用java提供的已有异常类————如:IOException、SQLException
114         //    2.2使用自定义的异常————创建一个继承Exception/RuntimeException的子类
115         
116         //3.异常的转译
117         //    3.1异常链————把原始的异常包装为心得异常类,并在新的异常类中封装原始异常类
118         //    3.2异常转型————相当于在捕获到异常后,将异常以新类型的异常抛出,使异常的信息更加直观
119         /*        如: public void test() throws MyException{ 
120                                 try{
121                                     ...
122                                 } catch(IOException e){ 
123                                     throw new MyException(); 
124                                     } 
125                                 finally{
126                                     ...
127                                 }
128                             }
129         */
130         try{
131             testDefinedException();//演示自定义异常类和异常转译
132         }catch(MyException e){
133             System.out.println("演示自定义异常类:" + e.toString());
134         }
135     }
136     
137     /*
138     *5.异常继承
139     */
140     public static void demoExceptionExtend(){
141         //1.父类的方法没有声明异常,子类在重写该方法时不能声明异常
142         //2.如果父类方法声明一个异常A,则子类在重写该方法时声明的异常不能是A的父类
143         //3.如果父类的方法声明的异常类型只有非运行时异常(运行时异常),则子类在重写该方法时声明的异常也只能是非运行时异常(运行时异常)
144         /*
145         总结就是:父类方法声明了什么异常,那么子类在重写该方法时只能是一样声明该异常或者该异常的子类
146         */
147     }
148     
149     /*
150     *6.异常小结
151     */
152     public static void demoExceptionNote(){
153         //根据度娘和工作经验得出异常的小结:
154         /*
155         1.只有在必要的时候才使用异常,因为捕获异常的代价是非常非常非常高的(没错,重要的词语要说三次)
156         2.不要用异常去控制程序的流程
157         3.不要使用空catch块
158         4.try块尽可能过小。保持一个try块对应一个或者多个异常
159         5.尽可能细化异常,别动不动就来个catch(Exception e)
160         6.catch块要保持一个块对应一类异常
161         7.不要把自己能处理的异常抛出
162         8.尽可能将检查异常转为非检查异常,然后交给上层处理
163         */
164     }
165     
166     /**演示TryCatch关键字**/
167     public static void testTryCatch(){
168         int a[] ={1,2,3};
169         try{
170             for(int i = 0;;i++){
171                 System.out.println("获取数组值:" + a[i]);
172             }
173         }catch(ArrayIndexOutOfBoundsException e){
174             System.out.println("Catch语句:" + "数组访问越界了");
175         }
176     }
177     
178     /**演示TryCatchFinaly关键字**/
179     public static void testTryCatchFinaly(){
180         int i = 0;
181         try{
182             int k = 1/i;
183         }catch(ArithmeticException e){
184             System.out.println("Catch语句:" + "除数为0");
185         }finally{
186             System.out.println("Finally语句:"+"除数都能为0?呵呵!");
187         }
188     }
189     
190     /**演示Throws关键字**/
191     public static void testThrows() throws ArithmeticException{
192         int i = 0;
193         int k = 1/i;
194     }
195     
196     /**演示Throw关键字**/
197     public static void testThrow(){
198         int i = 0;
199         int k = 1/i;
200         if( i==0){
201             throw new ArithmeticException();
202         }
203     }
204     
205     /**演示自定义异常类**/
206     public static void testDefinedException() throws MyException{
207         int i = 0;
208         try{
209             int k = 1/i;
210         }catch(ArithmeticException e){
211             throw new MyException("除数为0");
212         }
213     }
214 
215     
216     /*
217     *    抽取打印演示函数  用于打印演示功能提示
218     */    
219     public static void showDemo(String demoStr){
220         System.out.println("演示:" + demoStr);
221     }
222 }

 

 

 

第09章 异常

原文:http://www.cnblogs.com/ciade/p/4766806.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!