Execption异常 手动和自动抛除异常

 1 package cn.zmh.Exception;
 2 /*
 3 *
 4 * try{
 5 *   需要被检测的语句
 6 *   }
 7 *   catch(异常类  变量){
 8 *   异常的处理语句
 9 *   }
10 *   finally{
11 *   一定会被执行的语句
12 *   }
13 *
14 *   */
15 public class ExceptionDemo1 {
16     public static void main(String[] args) {
17         //手动抛除异常
18         try {
19             fun(0);
20         } catch (Exception e) {
21             e.printStackTrace();
22         }finally{
23             System.out.println("最终执行的代码");
24         }
25     }
26     // 自动抛除异常
27     public static void fun(int i) throws Exception {
28         if(i==0)
29         throw new Exception();
30         System.out.println(i);
31     }
32 
33 }

猜你喜欢

转载自www.cnblogs.com/zhangmenghui/p/10586496.html