V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
wyk1184
V2EX  ›  程序员

一个关于 java 的异常 很基础

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