java课后动手动脑

动手动脑Ⅰ

package org.example;

import java.util.Scanner;
import javax.swing.*;


public class Main {
    public static void main(String args[])
    {
        int i=1, j=0, k;
        k=i/j;
        try
        {

            k = i/j;    // Causes division-by-zero exception
            //throw new Exception("Hello.Exception!");
        }

        catch ( ArithmeticException e)
        {
            System.out.println("被0除.  "+ e.getMessage());
        }

        catch (Exception e)
        {
            if (e instanceof ArithmeticException)
                System.out.println("被0除");
            else
            {
                System.out.println(e.getMessage());

            }
        }
        finally
        {
            JOptionPane.showConfirmDialog(null,"OK");
        }

    }
}
显示的是

Exception in thread "main" java.lang.ArithmeticException: / by zero
at org.example.Main.main(Main.java:11)

可以发现除零操作


 

 

动手动脑Ⅱ:多层的异常捕获

 

 

 1 public class Main {
 2     public static void main(String[] args) {
 3         try {
 4             try {
 5                 throw new ArrayIndexOutOfBoundsException();
 6             }
 7             catch(ArrayIndexOutOfBoundsException e) {
 8                 System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch");
 9             }
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 }

运行结果为

ArrayIndexOutOfBoundsException/内层try-catch
发生ArithmeticException

 1 package org.example;
 2 
 3 import java.util.Scanner;
 4 import javax.swing.*;
 5 
 6 public class Main {
 7     public static void main(String[] args) {
 8         try {
 9             try {
10                 throw new ArrayIndexOutOfBoundsException();
11             }
12             catch(ArithmeticException e) {
13                 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
14             }
15             throw new ArithmeticException();
16         }
17         catch(ArithmeticException e) {
18             System.out.println("发生ArithmeticException");
19         }
20         catch(ArrayIndexOutOfBoundsException e) {
21             System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
22         }
23     }
24 }
结果为ArrayIndexOutOfBoundsException/外层try-catch

ArrayIndexOutOfBoundsException是数组下标越界异常
ArithmeticException数学运算异常

Java中多层异常捕获的运行规则如下:

1. 当程序执行到某个方法中可能抛出异常的语句时,会尝试执行该语句。
2. 如果在执行过程中发生了异常,会立即终止当前方法的执行,并跳转到匹配的catch块进行异常处理。
3. Java会按照catch块的顺序从上到下依次匹配异常类型,直到找到匹配的catch块。
4. 如果找到了匹配的catch块,会执行该catch块中的代码,并且整个异常捕获流程结束,程序继续执行catch块之后的代码。
5. 如果没有找到匹配的catch块,当前方法会立即结束,并将异常传递给调用它的方法进行处理。
6. 如果调用方法也没有捕获到异常,异常会继续向上层调用方法传递,直到找到匹配的catch块进行处理,或者直到达到程序的最顶层(例如main方法),如果最顶层也没有捕获到异常,程序会终止并打印异常信息。

在多层异常捕获中,内层的catch块会先尝试匹配异常类型,如果找到匹配的catch块,则不会继续向外层传递异常。而外层的catch块只有在内层的catch块没有匹配到异常类型时才会执行。

需要注意的是,异常捕获是按照catch块的顺序匹配异常类型的,因此在多层异常捕获中,应该将特殊的异常类型放在前面,将通用的异常类型放在后面,以确保异常能够被正确地捕获和处理。

另外,还可以使用多个catch块来处理不同类型的异常,也可以使用一个catch块来处理多个异常类型,使用管道符(|)将多个异常类型连接在一起。

总之,多层异常捕获可以帮助我们根据具体的异常类型,选择不同的处理方式,提高程序的容错性和稳定性。



动手动脑Ⅲ

 1 package org.example;
 2 
 3 import java.util.Scanner;
 4 import javax.swing.*;
 5 
 6 
 7 public class Main {
 8 
 9 
10     public static void main(String args[]) {
11 
12         int result;
13 
14         try {
15 
16             System.out.println("in Level 1");
17 
18 
19             try {
20 
21                 System.out.println("in Level 2");
22                 // result=100/0;  //Level 2
23 
24                 try {
25 
26                     System.out.println("in Level 3");
27 
28                     result=100/0;  //Level 3
29 
30                 }
31 
32                 catch (Exception e) {
33 
34                     System.out.println("Level 3:" + e.getClass().toString());
35 
36                 }
37 
38 
39                 finally {
40 
41                     System.out.println("In Level 3 finally");
42 
43                 }
44 
45 
46                 // result=100/0;  //Level 2
47 
48 
49             }
50 
51             catch (Exception e) {
52 
53                 System.out.println("Level 2:" + e.getClass().toString());
54 
55             }
56             finally {
57 
58                 System.out.println("In Level 2 finally");
59 
60             }
61 
62             // result = 100 / 0;  //level 1
63 
64         }
65 
66         catch (Exception e) {
67 
68             System.out.println("Level 1:" + e.getClass().toString());
69 
70         }
71 
72         finally {
73 
74          System.out.println("In Level 1 finally");
75 
76         }
77 
78     }
79 
80 }
输出

in Level 1
in Level 2
in Level 3
Level 3:class java.lang.ArithmeticException
In Level 3 finally
In Level 2 finally
In Level 1 finally

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

 

 1 package org.example;
 2 
 3 import java.util.Scanner;
 4 import javax.swing.*;
 5 
 6 
 7 
 8 public class Main {
 9 
10 
11     public static void main(String[] args)
12     {
13 
14         try{
15 
16 
17             System.out.println("in main");
18 
19             throw new Exception("Exception is thrown in main");
20 
21             //System.exit(0);
22 
23 
24         }
25 
26         catch(Exception e)
27 
28         {
29 
30             System.out.println(e.getMessage());
31 
32             System.exit(0);
33 
34         }
35 
36         finally
37 
38         {
39 
40             System.out.println("in finally");
41 
42         }
43 
44     }
45 
46 
47 }

输出

in main
Exception is thrown in main

 

直接system.exit(0)退出程序

 

posted on 2023-10-19 23:41  临江柔  阅读(20)  评论(0)    收藏  举报