动手动脑(课堂作业06)

1,请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识。

 1 import javax.swing.*;
 2 
 3 class AboutException {
 4    public static void main(String[] a) 
 5    {
 6       int i=1, j=0, k;
 7       k=i/j;
 8 
 9 
10     try
11     {
12         
13         k = i/j;    // Causes division-by-zero exception
14         //throw new Exception("Hello.Exception!");
15     }
16     
17     catch ( ArithmeticException e)
18     {
19         System.out.println("被0除.  "+ e.getMessage());
20     }
21     
22     catch (Exception e)
23     {
24         if (e instanceof ArithmeticException)
25             System.out.println("被0除");
26         else
27         {  
28             System.out.println(e.getMessage());
29             
30         }
31     }
32 
33     
34     finally
35      {
36              JOptionPane.showConfirmDialog(null,"OK");
37      }
38         
39   }
40 }

运行结果为:

不难看出,这是程序出错,但这并没有抛出异常,程序运行到第一个k=i/j就出错了,将其注释掉后运行结果为:

Java中的异常捕获语句:

Try

{

//可能发生运行错误的代码;

}

catch(异常类型 异常对象引用)

{

//用于处理异常的代码

}

finally

{

//用于“善后” 的代码

}

 Java 中所有可捕获的异常都派生自 Exception 类。

2,

使用javap去反汇编两个示例程序的.class文件

第一个反编译得:

 

第二个反编译得:

 

3,阅读以下代码(CatchWho.java),写出程序运行结果:

 1 public class CatchWho { 
 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

我认为try里面的代码throw new ArrayIndexOutOfBoundsException先抛出ArrayIndexOutOfBoundsException类型的异常,于是System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch");之后throw new ArithmeticException();于是System.out.println("发生ArithmeticException");

运行结果:

4,写出CatchWho2.java程序运行的结果

 1 public class CatchWho2 { 
 2     public static void main(String[] args) { 
 3         try {
 4                 try { 
 5                     throw new ArrayIndexOutOfBoundsException(); 
 6                 } 
 7                 catch(ArithmeticException e) { 
 8                     System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 
 9                 }
10             throw new ArithmeticException(); 
11         } 
12         catch(ArithmeticException e) { 
13             System.out.println("发生ArithmeticException"); 
14         } 
15         catch(ArrayIndexOutOfBoundsException e) { 
16             System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
17         } 
18     } 
19 }

预测结果:

ArrayIndexOutOfBoundsException/外层try-catch

我认为throw new ArrayIndexOutOfBoundsException();之后就到了System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 不再往回运行

运行结果:

5,

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

预测结果:

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

程序一个一个try往下运行,于是输出in Level 1
in Level 2
in Level 3

 因为result=100/0;所以出了异常输出Level 3:class java.lang.ArithmeticException

紧接着另外两个try都没出异常正常运行finally依次输出

In Level 3 finally
In Level 2 finally
In Level 1 finally

运行结果:

6,

 1 public class SystemExitAndFinally {
 2 
 3     
 4     public static void main(String[] args)
 5     {
 6         
 7         try{
 8 
 9             
10             System.out.println("in main");
11             
12             throw new Exception("Exception is thrown in main");
13 
14                     //System.exit(0);
15 
16         
17         }
18         
19         catch(Exception e)
20 
21             {
22             
23             System.out.println(e.getMessage());
24             
25             System.exit(0);
26         
27         }
28         
29         finally
30         
31         {
32             
33             System.out.println("in finally");
34         
35         }
36     
37     }
38 
39 
40 }

 运行结果:

原因:因为程序里有System.exit(0);,运行完catch就结束程序运行了。

7,如何跟踪异常的传播路径?

当程序中出现异常时,JVM会依据方法调用顺序依次查找有关的错误处理程序。 可使用printStackTrace 和 getMessage方法了解异常发生的情况: printStackTrace:打印方法调用堆栈。 每个Throwable类的对象都有一个getMessage方法,它返回一个字串,这个字串是在Exception构造函数中传入的,通常让这一字串包含特定异常的相关信息。

// UsingExceptions.java
// Demonstrating the getMessage and printStackTrace
// methods inherited into all exception classes.
public class PrintExceptionStack {
   public static void main( String args[] )
   {
      try {
         method1();
      }
      catch ( Exception e ) {
         System.err.println( e.getMessage() + "
" );
         e.printStackTrace();
      }
   }

   public static void method1() throws Exception
   {
      method2();
   }

   public static void method2() throws Exception
   {
      method3();
   }

   public static void method3() throws Exception
   {
      throw new Exception( "Exception thrown in method3" );
   }
}

运行结果:

8,

因为他没有添加异常抛出:

原文地址:https://www.cnblogs.com/chch157/p/7847159.html