• 请不要在回答技术问题时复制粘贴 AI 生成的内容
wyk1184
V2EX  ›  程序员

一个关于 java 的异常 很基础

  •  
  •   wyk1184 · Jul 18, 2014 · 4850 views
    This topic created in 4492 days ago, the information mentioned may be changed or developed.
    public class MultiCatch {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    try{
    int a =args.length;
    System.out.println(a);
    int b=2321/a;
    int c[]={1};
    c[42]=44;
    }catch(ArithmeticException e){
    System.out.println(e);}
    catch(ArrayIndexOutOfBoundsException e){
    System.out.println(e);
    System.out.println("this line is out of work");
    }
    }
    }

    //////////////
    以上是代码 应该是有两个异常 一个是 ArithmeticException
    一个是 ArrayIndexOutOfBoundsExceptions
    但是第二个catch怎么也抓不住这个异常
    结构仅显示
    0
    java.lang.ArithmeticException: / by zero



    求大神赐教啊
    11 replies  •  2014-07-20 10:12:49 +08:00
    dorentus
    dorentus
    Jul 18, 2014❤️1
    1
    2321 除以 0,抛出 ArithmeticException 之后就结束了,“int b=2321/a;”这行之后的代码不会被执行到,自然不再会抛出其它异常。
    cpp255
    cpp255
    Jul 18, 2014❤️1
    2
    int b=2321/a; // 这里已经抛出了 ArithmeticException 异常,程序跳出 try 了,下面的代码就不会继续运行了
    ```
    int c[]={1};
    c[42]=44;
    ```
    wyk1184
    wyk1184
    OP
    Jul 18, 2014
    3
    @dorentus
    就是说只执行第一个异常吧 ,catch之后 后面的代码也不执行了 异常也不catch了对吧
    wyk1184
    wyk1184
    OP
    Jul 18, 2014
    4
    @cpp255
    多谢
    wyk1184
    wyk1184
    OP
    Jul 18, 2014
    5
    @dorentus 多谢
    cha1
    cha1
    Jul 18, 2014❤️2
    6
    单步了一下:
    cha1
    cha1
    Jul 18, 2014
    7
    cha1
    cha1
    Jul 18, 2014
    8
    (不好意思.第一次在v2ex发图.使用了围脖是个好图床,可是好像有点问题.....)
    vainly
    vainly
    Jul 19, 2014 via iPad❤️1
    9
    @zqhong 楼上代码录制用什么软件啊
    cha1
    cha1
    Jul 19, 2014❤️2
    10
    @vainly Linux -- byzanz-record, windows -- GifCam(http://blog.bahraniapps.com/gifcam/)
    tongchengpp
    tongchengpp
    Jul 20, 2014
    11
    @zqhong 好东西 谢谢~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1313 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 68ms · UTC 21:06 · PVG 05:06 · LAX 13:06 · JFK 16:06
    ♥ Do have faith in what you're doing.