第五章:异常

木婉清2023/12/21

1、

image-20231221163135578

程序运行结果

Before exception!

a[8] = 12;

上述代码出错,程序停止运行,因此只输出了 Before exception!

2、

image-20231221163211760

程序运行结果

Before exception!

ArrayIndexOutOfBoundsException caught!

End up!

注意: 此处不抛出ArithmeticException 异常,是因为它是一种在 Java 或其他编程语言中执行算术操作时遇到溢出或零溢出等特殊情况时抛出的异常类型。

3、

image-20231221163245170

程序运行结果

No!

End up!

此处未发生 ArrayIndexOutOfBoundsException 异常,是因为它是一种在尝试使用超出数组界限的索引访问数组元素时抛出的异常类型。

4、

image-20231221163317189

程序运行结果

6/3 is 2

Can't divide by zero!

16/4 is 4

25/5 is 5

Can't divide by zero!

48/8 is 6

No matching element found!

No matching element found!

5、

image-20231221163407327

程序运行结果

6/3 is 2

Can't divide by zero!

6、

image-20231221163436930

程序运行结果

Before throw!

ArithmeticException caught!

End up!

7、

image-20231221163502737

程序运行结果

step 1.

step 2.

step 3.

my exceptionstep 4.

step 5.

step 6.

8、

image-20231221163533062

程序运行结果

step 1.

step 2.

step 5.

step 6.

没有抛出异常,故不执行 catch 语句

9、

image-20231221163604374

程序运行结果

just test

编辑于 2023/12/22 10:52:59