java exception 二次抛出_java之异常处理

AboutException的测试:

1 packagetest;2 import javax.swing.*;3

4 classAboutException {5 public static voidmain(String[] a)6 {7 @SuppressWarnings("unused")8 int i=1, j=0, k;9 //System.out.println("第一次直接对两个整数进行除法运算结果:");10 //k=i/j;

11

12

13 try

14 {15 System.out.println("第五次测试finally语句在没有报错的前提下以及提前退出语句下是否会被执行:");16 //System.out.println("第二次将两个整数放在try{}catch{}函数中进行除法运算结果:");

17 k = i/j; //Causes division-by-zero exception18 //throw new Exception("Hello.Exception!");19 //System.out.println("第三次测试finally语句在没有报错的情况下是否会被执行:");

20

21 }22

23

24

25 catch(Exception e)26 {27

28 System.out.println(e.getMessage());29 System.exit(0);30

31 }32

33

34 finally

35 {36 JOptionPane.showConfirmDialog(null,"OK");37 }38

39 }40 }

1.第一次

c2fbc8feecbfe109f44b3ddc036299ba.png

34db45c1acfe894dd33d101184c282be.png

因为0不能作为分母而报错。

第二次

fe0171fd461225ed6a957d4a325788b5.png

可以看到在try{}catch{}函数中虽然没有报错,但运算结果是将报错信息输出:/ by zero,

可以看出try{} catch{}函数的作用,还可以看到错误的信息。

第三次

8b9428a01359e0158688b560b28d7829.png

eb5f8756d744b098e1a7a92519b4aa99.png

可以看到,在程序没有报错的情况下,finally任然会被执行。

第四次

22f324fe89909d0190bd88424a6adf10.png

769949497962ff2a69c7b54f8354fa3c.png

可以看到,在报错的前提下,加入提前退出语句后只输出了错误信息,而没有执行finally语句。

第五次

4a900e9cf9aad38603398f934fbba517.png

可以看到,在没有报错的前提下,catch语句不能正常执行,所以提前退出语句不执行,finally语句得以正常执行。

综上所述,有

(1)Finally语句不管有没有异常发生,finally语句都得以运行,但是在遇到提前退出语句后不能被执行。

(2)try{}catch{}函数能将错误信息抛出.

ThrowDemo的测试:

1 packagetest;2 public classThrowDemo {3 public static voidmain(String[] args) {4 //try {

5 double data = 100 / 0.0;6 System.out.println("浮点数除以零:" +data);7 //if(String.valueOf(data).equals("Infinity"))8 //{9 //System.out.println("In Here" );10 //throw new ArithmeticException("除零异常");11 //}12 //}13 //catch(ArithmeticException e) {14 //System.out.println(e);15 //}

16 }17 }18

1.第一次测试

d5ac14c9f5f19df7cffc3d819617fb90.png

没有报错,但是如果将0.0改为0就会有如下结果:

edb65aec890aef82487a26426bae7ede.png

这个错误相比不是很陌生了,因为在上一个测试中我们遇到了无数次这个错误类型,就是因为分母为零,根据Java对double的定义运算,结果为无限。

2.第二次测试

ced5b628892f3f7843b7808833baf6ce.png

系统抛出新的错误被截取,

3.第三次测试

53318e263a7e0c050dba203b4cbe8489.png

错误被截取后正常输出。

d0645b8b3d135844688fb3c8e9aa5b15.png

在这个测试里,第二个catch没有被编译执行。

CatchWho测试:

1 packagetest;2 public classCatchWho {3 public static voidmain(String[] args) {4 try { //2

5 try { //1

6 throw newArrayIndexOutOfBoundsException();7 }8 catch(ArrayIndexOutOfBoundsException e) {9 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");10 }11

12 throw new ArithmeticException(); //扔出新的错误

13 }14 catch(ArithmeticException e) {15 System.out.println("发生ArithmeticException");16 }17 catch(ArrayIndexOutOfBoundsException e) { //没有输出,因为前面有一个catch将错误捕捉,这个就不会被执行了

18 System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");19 }20 }21 }

测试结果

1dc28165263035dce70c183b0868ab5e.png

抛出错误类型。

CatchWho2测试:

1 packagetest;2 public classCatchWho2 {3 public static voidmain(String[] args) {4 try{5 try { //第一次扔出错误

6 throw newArrayIndexOutOfBoundsException();7 }8 catch(ArithmeticException e) {//截取错误,但没有输出

9 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");10 }11 throw new ArithmeticException(); //第二次扔出错误

12 }13 catch(ArithmeticException e) { //不是正确的错误类型,错误类型不匹配,不输出

14 System.out.println("发生ArithmeticException");15 }16 catch(ArrayIndexOutOfBoundsException e) { //截取错误类型

17 System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");18 }19 }20 }

EmbededFinally测试;

1 packagetest;2 public classEmbededFinally {3

4

5 public static voidmain(String args[]) {6

7 intresult;8

9 try{10 //result=100/0;

11 System.out.println("in Level 1");12

13

14 try{15 result=100/0;16 System.out.println("in Level 2");17 //result=100/0;//Level 2

18

19 try{20

21 System.out.println("in Level 3");22

23 result=100/0; //Level 3

24

25 }26

27 catch(Exception e) {28

29 System.out.println("Level 3:" +e.getClass().toString());30

31 }32

33

34 finally{35

36 System.out.println("In Level 3 finally");37

38 }39

40

41 //result=100/0;//Level 2

42

43

44 }45

46 catch(Exception e) {47

48 System.out.println("Level 2:" +e.getClass().toString());49

50 }51 finally{52

53 System.out.println("In Level 2 finally");54

55 }56

57 //result = 100 / 0;//level 1

58

59 }60

61 catch(Exception e) {62

63 System.out.println("Level 1:" +e.getClass().toString());64

65 }66

67 finally{68

69 System.out.println("In Level 1 finally");70

71 }72

73 }74

75 }

测试结果:

521db119e44e0ccfcd49c9c57aae376e.png

总结:

当有多层嵌套的finally时,异常在不同的层次抛出,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值