V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wyk1184
V2EX  ›  程序员

一个关于 java 的异常 很基础

  •  
  •   wyk1184 · 2014-07-18 16:28:16 +08:00 · 3374 次点击
    这是一个创建于 3569 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复    2014-07-20 10:12:49 +08:00
    dorentus
        1
    dorentus  
       2014-07-18 16:34:44 +08:00   ❤️ 1
    2321 除以 0,抛出 ArithmeticException 之后就结束了,“int b=2321/a;”这行之后的代码不会被执行到,自然不再会抛出其它异常。
    cpp255
        2
    cpp255  
       2014-07-18 16:36:32 +08:00   ❤️ 1
    int b=2321/a; // 这里已经抛出了 ArithmeticException 异常,程序跳出 try 了,下面的代码就不会继续运行了
    ```
    int c[]={1};
    c[42]=44;
    ```
    wyk1184
        3
    wyk1184  
    OP
       2014-07-18 16:39:44 +08:00
    @dorentus
    就是说只执行第一个异常吧 ,catch之后 后面的代码也不执行了 异常也不catch了对吧
    wyk1184
        4
    wyk1184  
    OP
       2014-07-18 16:40:02 +08:00
    @cpp255
    多谢
    wyk1184
        5
    wyk1184  
    OP
       2014-07-18 16:40:21 +08:00
    @dorentus 多谢
    zqhong
        6
    zqhong  
       2014-07-18 20:28:04 +08:00   ❤️ 2
    单步了一下:
    zqhong
        7
    zqhong  
       2014-07-18 20:36:51 +08:00
    zqhong
        8
    zqhong  
       2014-07-18 22:17:14 +08:00
    (不好意思.第一次在v2ex发图.使用了围脖是个好图床,可是好像有点问题.....)
    vainly
        9
    vainly  
       2014-07-19 09:40:53 +08:00 via iPad   ❤️ 1
    @zqhong 楼上代码录制用什么软件啊
    zqhong
        10
    zqhong  
       2014-07-19 10:02:48 +08:00   ❤️ 2
    @vainly Linux -- byzanz-record, windows -- GifCam(http://blog.bahraniapps.com/gifcam/)
    tongchengpp
        11
    tongchengpp  
       2014-07-20 10:12:49 +08:00
    @zqhong 好东西 谢谢~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3161 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:55 · PVG 22:55 · LAX 07:55 · JFK 10:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.