课堂作业(异常处理)

一、动手动脑 运行AboutException.java示例,了解Java中实现异常处理的基础知识。

(1)源代码

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

(2)结果截图

(3)结果分析

Try{
   //可能发生运行错误的代码;
  }
  catch(异常类型     异常对象引用){
   //用于处理异常的代码
  }
  finally{
   //用于“善后” 的代码
  }

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

二、使用Java异常处理机制

•把可能会发生错误的代码放进try语句块中。
•当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。catch语句块中的代码用于处理错误。
•当异常发生时,程序控制流程由try语句块跳转到catch语句块。
•不管是否有异常发生,finally语句块中的语句始终保证被执行。
•如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。

三、动手动脑  多层的异常捕获-1

(1)源代码

 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             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 }

(2)结果截图

(3)结果分析
抛出两次异常,第一次处理完毕后,再处理第二次。

四、动手动脑  多层的异常捕获-2

(1)源代码

 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 }

(2)结果截图

(3)结果分析

一个异常处理完后,才能抛出下一个异常。

五、动手动脑  当有多个嵌套的try…catch…finally时,要特别注意finally的执行时机。

(1)源代码

 1 public class EmbededFinally {
 2     public static void main(String args[]) {
 3         int result;       
 4         try {          
 5             System.out.println("in Level 1");
 6              try {                
 7                 System.out.println("in Level 2");
 8   // result=100/0;  //Level 2
 9                  try {                   
10                      System.out.println("in Level 3");                     
11                      result=100/0;  //Level 3                
12                 } 
13                 
14                 catch (Exception e) {
15                     System.out.println("Level 3:" + e.getClass().toString());                
16                 }
17                 
18                 finally {
19                     System.out.println("In Level 3 finally");                
20                 }              
21                 // result=100/0;  //Level 2            
22                 }          
23             catch (Exception e) {               
24                  System.out.println("Level 2:" + e.getClass().toString());           
25              }
26              finally {                
27                 System.out.println("In Level 2 finally");          
28              }             
29             // result = 100 / 0;  //level 1        
30         }         
31         catch (Exception e) {           
32             System.out.println("Level 1:" + e.getClass().toString());        
33         }        
34         finally {
35          System.out.println("In Level 1 finally");        
36         }    
37     }
38 }

(2)结果截图

(3)结果分析

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

六、动手动脑  finally语句块一定会执行吗?

(1)源程序

 1 public class SystemExitAndFinally {   
 2     public static void main(String[] args)
 3     {        
 4         try{
 5             System.out.println("in main");
 6             throw new Exception("Exception is thrown in main");           
 7                //System.exit(0);
 8         }
 9         catch(Exception e)
10             {            
11             System.out.println(e.getMessage());  
12             System.exit(0);
13         }
14         finally
15         {            
16             System.out.println("in finally");
17         }    
18     }
19 }

(2)结果截图

(3)结果分析

不一定。因为当运行 System.exit(0);时,终止了JAVA虚拟机,导致不能执行finally的内容。

原文地址:https://www.cnblogs.com/ghs1065248758/p/6101572.html