第09章 异常

  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         
 85         
 86         //2.throw throws
 87         //    2.1 throw关键字  抛出一个异常对象  只会出现在方法体内
 88         //    2.2 throws关键字  出现在方法的声明中,表示可能会抛出的异常,可允许后面跟着多个异常类型
 89         showDemo("演示throws");
 90         try{
 91             testThrows();//演示throws
 92         }catch(ArithmeticException e){
 93             System.out.println("除数都能为0?哥笑了!");
 94         }
 95         
 96         showDemo("演示throw");
 97         try{
 98             testThrow();//演示throw
 99         }catch(ArithmeticException e){
100             System.out.println("除数都能为0?大哥我笑了!");
101         }
102     }
103     
104     /*
105     *4.异常处理机制
106     */
107     public static void demoExceptionHandle(){
108         //1.对于可能出现异常的代码,处理方式有两种
109         //    1.1处理异常————使用try..catch语句对异常进行处理
110         //        如:  try{ ...} catch(异常类型1){} catch(异常类型2){} catch(异常类型3){} finally{}
111         //    1.2不处理异常————对于处理不了的异常或者需要转型的异常在方法声明处使用throws语句进行异常的抛出
112         //        如:public void testException() throws Exception{ if(...) { throw new Exception();}}
113         
114         //2.使用异常类的方式有两种:
115         //    2.1使用java提供的已有异常类————如:IOException、SQLException
116         //    2.2使用自定义的异常————创建一个继承Exception/RuntimeException的子类
117         
118         //3.异常的转译
119         //    3.1异常链————把原始的异常包装为新的异常类,并在新的异常类中封装原始异常类
120         /*  如:
121                 public void test(){
122                     try{
123                         .....
124                     }catch(ArithmeticException ex){
125                          System.out.println("Arithmetic exception occoured: "+ex);
126                          try {
127                              throw new NumberFormatException();
128                          }catch(NumberFormatException ex1) {
129                             System.out.println("Chained exception thrown manually : "+ex1);
130                          }
131                     }
132                 }
133         
134         */
135         //    3.2异常转型————相当于在捕获到异常后,将异常以新类型的异常抛出,使异常的信息更加直观
136         /*        如: 
137         public void test() throws MyException{ 
138                                 
139             try{
140                 ...
141             } catch(IOException e){ 
142               throw new MyException(); 
143               } 
144             finally{
145                 ...
146                 }
147         }
148         */
149         try{
150             testDefinedException();//演示自定义异常类和异常转译
151         }catch(MyException e){
152             System.out.println("演示自定义异常类:" + e.toString());
153         }
154     }
155     
156     /*
157     *5.异常继承
158     */
159     public static void demoExceptionExtend(){
160         //1.父类的方法没有声明异常,子类在重写该方法时不能声明异常
161         //2.如果父类方法声明一个异常A,则子类在重写该方法时声明的异常不能是A的父类
162         //3.如果父类的方法声明的异常类型只有非运行时异常(运行时异常),则子类在重写该方法时声明的异常也只能是非运行时异常(运行时异常)
163         /*
164         总结就是:父类方法声明了什么异常,那么子类在重写该方法时只能是一样声明该异常或者该异常的子类
165         */
166     }
167     
168     /*
169     *6.异常小结
170     */
171     public static void demoExceptionNote(){
172         //根据度娘和工作经验得出异常的小结:
173         /*
174         1.只有在必要的时候才使用异常,因为捕获异常的代价是非常非常非常高的(没错,重要的词语要说三次)
175         2.不要用异常去控制程序的流程
176         3.不要使用空catch块
177         4.try块尽可能过小。保持一个try块对应一个或者多个异常
178         5.尽可能细化异常,别动不动就来个catch(Exception e)
179         6.catch块要保持一个块对应一类异常
180         7.不要把自己能处理的异常抛出
181         8.尽可能将检查异常转为非检查异常,然后交给上层处理
182         */
183     }
184     
185     /**演示TryCatch关键字**/
186     public static void testTryCatch(){
187         int a[] ={1,2,3};
188         try{
189             for(int i = 0;;i++){
190                 System.out.println("获取数组值:" + a[i]);
191             }
192         }catch(ArrayIndexOutOfBoundsException e){
193             System.out.println("Catch语句:" + "数组访问越界了");
194         }
195     }
196     
197     /**演示TryCatchFinaly关键字**/
198     public static void testTryCatchFinaly(){
199         int i = 0;
200         try{
201             int k = 1/i;
202         }catch(ArithmeticException e){
203             System.out.println("Catch语句:" + "除数为0");
204         }finally{
205             System.out.println("Finally语句:"+"除数都能为0?呵呵!");
206         }
207     }
208     
209     /**演示Throws关键字**/
210     public static void testThrows() throws ArithmeticException{
211         int i = 0;
212         int k = 1/i;
213     }
214     
215     /**演示Throw关键字**/
216     public static void testThrow(){
217         int i = 0;
218         int k = 1/i;
219         if( i==0){
220             throw new ArithmeticException();
221         }
222     }
223     
224     /**演示自定义异常类**/
225     public static void testDefinedException() throws MyException{
226         int i = 0;
227         try{
228             int k = 1/i;
229         }catch(ArithmeticException e){
230             throw new MyException("除数为0");
231         }
232     }
233 
234     
235     /*
236     *    抽取打印演示函数  用于打印演示功能提示
237     */    
238     public static void showDemo(String demoStr){
239         System.out.println("演示:" + demoStr);
240     }
241 }

 

posted @ 2015-08-28 16:04  ciade  阅读(127)  评论(0编辑  收藏  举报