zoukankan      html  css  js  c++  java
  • 异常-try...catch的方式处理异常2

      1 package cn.itcast_02;
      2 
      3 /*
      4  * A:一个异常
      5  * B:二个异常的处理
      6  *         a:每一个写一个try...catch
      7  *         b:写一个try,多个catch
      8  *             try{
      9  *                 ...
     10  *             }catch(异常类名 变量名) {
     11  *                 ...
     12  *             }
     13  *             catch(异常类名 变量名) {
     14  *                 ...
     15  *             }
     16  *             ...
     17  * 
     18  *             注意事项:
     19  *                 1:能明确的尽量明确,不要用大的来处理。
     20  *                 2:平级关系的异常谁前谁后无所谓,如果出现了子父关系,父必须在后面。
     21  * 
     22  * 注意:
     23  *         一旦try里面出了问题,就会在这里把问题给抛出去,然后和catch里面的问题进行匹配,
     24  *         一旦有匹配的,就执行catch里面的处理,然后结束了try...catch,继续执行后面的语句。
     25  */
     26 public class ExceptionDemo2 {
     27     public static void main(String[] args) {
     28         
     29         // method1();
     30 
     31         // method2();
     32 
     33         // method3();
     34 
     35         method4();
     36     }
     37 
     38     public static void method4() {
     39         int a = 10;
     40         int b = 0;
     41         int[] arr = { 1, 2, 3 };
     42 
     43         // 爷爷在最后
     44         try {
     45             System.out.println(a / b);
     46             System.out.println(arr[3]);
     47             System.out.println("这里出现了一个异常,你不太清楚是谁,该怎么办呢?");
     48         } catch (ArithmeticException e) {
     49             System.out.println("除数不能为0");
     50         } catch (ArrayIndexOutOfBoundsException e) {
     51             System.out.println("你访问了不该的访问的索引");
     52         } catch (Exception e) {//它可以匹配所有的异常
     53             System.out.println("出问题了");
     54         }
     55         
     56         System.out.println("over");
     57 
     58         /* 
     59          try {
     60             System.out.println(a / b);
     61             System.out.println(arr[3]);
     62             System.out.println("这里出现了一个异常,你不太清楚是谁,该怎么办呢?");
     63         } catch (Exception e) {//爷爷在前面是不可以的
     64             System.out.println("出问题了");
     65         } catch (ArithmeticException e) {
     66             System.out.println("除数不能为0");
     67         } catch (ArrayIndexOutOfBoundsException e) {
     68             System.out.println("你访问了不该的访问的索引");
     69         }
     70 
     71         System.out.println("over");*/
     72     }
     73 
     74     // 两个异常的处理,写一个try,多个catch
     75     public static void method3() {
     76         int a = 10;
     77         int b = 0;
     78         int[] arr = { 1, 2, 3 };
     79 
     80         try {
     81             System.out.println(arr[3]);
     82             System.out.println(a / b);
     83             // System.out.println(arr[3]);
     84         } catch (ArithmeticException e) {
     85             System.out.println("除数不能为0");
     86         } catch (ArrayIndexOutOfBoundsException e) {
     87             System.out.println("你访问了不该的访问的索引");
     88         }
     89 
     90         System.out.println("over");
     91     }
     92 
     93     // 两个异常的处理,每一个写一个try...catch
     94     public static void method2() {
     95         int a = 10;
     96         int b = 0;
     97         try {
     98             System.out.println(a / b);
     99         } catch (ArithmeticException e) {
    100             System.out.println("除数不能为0");
    101         }
    102 
    103         int[] arr = { 1, 2, 3 };
    104         try {
    105             System.out.println(arr[3]);
    106         } catch (ArrayIndexOutOfBoundsException e) {
    107             System.out.println("你访问了不该的访问的索引");
    108         }
    109 
    110         System.out.println("over");
    111     }
    112 
    113     // 一个异常的处理
    114     public static void method1() {
    115         // 第一阶段
    116         int a = 10;
    117         // int b = 2;
    118         int b = 0;
    119 
    120         try {
    121             System.out.println(a / b);
    122         } catch (ArithmeticException ae) {
    123             System.out.println("除数不能为0");
    124         }
    125 
    126         // 第二阶段
    127         System.out.println("over");
    128     }
    129 }

     

  • 相关阅读:
    PHP全部手册
    你必须收藏的GitHub技巧
    PV和并发
    api接口
    LeetCode 14. 最长公共前缀
    LeetCode 1037. 有效的回旋镖
    LeetCode 242. 有效的字母异位词
    LeetCode 151. 翻转字符串里的单词
    LeetCode 22. 括号生成
    LeetCode 面试题05. 替换空格
  • 原文地址:https://www.cnblogs.com/ZHOUVIP/p/7220668.html
Copyright © 2011-2022 走看看