一个关于 java 的异常 很基础

2014-07-18 16:28:16 +08:00
 wyk1184
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



求大神赐教啊
3388 次点击
所在节点    程序员
11 条回复
dorentus
2014-07-18 16:34:44 +08:00
2321 除以 0,抛出 ArithmeticException 之后就结束了,“int b=2321/a;”这行之后的代码不会被执行到,自然不再会抛出其它异常。
cpp255
2014-07-18 16:36:32 +08:00
int b=2321/a; // 这里已经抛出了 ArithmeticException 异常,程序跳出 try 了,下面的代码就不会继续运行了
```
int c[]={1};
c[42]=44;
```
wyk1184
2014-07-18 16:39:44 +08:00
@dorentus
就是说只执行第一个异常吧 ,catch之后 后面的代码也不执行了 异常也不catch了对吧
wyk1184
2014-07-18 16:40:02 +08:00
@cpp255
多谢
wyk1184
2014-07-18 16:40:21 +08:00
@dorentus 多谢
zqhong
2014-07-18 20:28:04 +08:00
单步了一下:
zqhong
2014-07-18 20:36:51 +08:00
zqhong
2014-07-18 22:17:14 +08:00
(不好意思.第一次在v2ex发图.使用了围脖是个好图床,可是好像有点问题.....)
vainly
2014-07-19 09:40:53 +08:00
@zqhong 楼上代码录制用什么软件啊
zqhong
2014-07-19 10:02:48 +08:00
@vainly Linux -- byzanz-record, windows -- GifCam(http://blog.bahraniapps.com/gifcam/)
tongchengpp
2014-07-20 10:12:49 +08:00
@zqhong 好东西 谢谢~

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/123348

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX