完成课件中的动手动脑的或需要验证的相关内容。 (截止时间2017-11-16晚23:00)

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

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

  代码执行时不发生异常。

  jvm在处理浮点数时,生成的是ddiv字节码指令,i/0,0转化为浮点数0.0,而0.0是double类型的,并不精确,所以不会抛出异常。

  jvm在处理整数时,生成的是idiv字节码指令,整数除0就是除0,会抛出异常。

异常处理:Java中异常捕获语句

 1 try
 2 {
 3     //用于监控可能发生错误的语句
 4 }
 5 catch(异常类型 异常对象引用)
 6 {
 7     //用于捕获并处理异常的代码
 8 }
 9 finally
10 {
11     //用于“善后” 的代码
12 }

  不管是否有异常发生,finally语句块中的语句始终保证被执行。

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

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

运行结果:

  ArrayIndexOutOfBoundsException/内层try-catch
  发生ArithmeticException

截图:

   

3>写出CatchWho2.java程序运行的结果

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

运行结果:

  ArrayIndexOutOfBoundsException/外层try-catch

截图:

   

结果分析:

  当异常未被处理时无法接受新的异常

4>请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。

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

 运行结果:

   

   

结果分析

  当外层异常未被处理时,内层异常不会被处理并且finally也不会执行,当有多层嵌套的finally语句时,异常在不同层次不同位置抛出时,也会导致不同的finally语句块执行顺序。

5>finally语句块一定会执行吗?

  请通过 SystemExitAndFinally.java示例程序回答上述问题。

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

运行结果:

   

   

  首先只有与finally对应的try语句得到执行的情况下finally语句才会执行,但如果finally语句之前出现例如System.exit(0) 等使Java虚拟机停止运行的语句时finally语句也不会被执行。

6>编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。

 1 import java.util.Scanner;
 2 class MyException extends Exception
 3 {
 4     MyException(String str)
 5     {
 6         super(str);
 7     }
 8 }
 9 class GradeTest
10 {
11     private double grade;
12     public void InputGrade(String s)
13     {
14         boolean flag = true;
15         try
16         {
17             for(int i = 0;i<s.length();i++)
18             {
19                 if((s.charAt(i) < '0' || s.charAt(i) > '9')&& s.charAt(i) != '.')
20                 flag = false;
21             }
22             if(!flag)
23            {
24                throw new MyException("请输入数字!");
25            }
26            try
27 
28            {
29                grade = Double.parseDouble(s);
30                if(grade < 0.0 || grade > 100.0)
31                {
32                    throw new MyException("请输入0.0到100.0的数据!");
33                }
34                if(grade < 60.0)
35                    System.out.println("不及格");
36                else if(grade <70.0)
37                    System.out.println("及格");
38                else if(grade < 80.0)
39                    System.out.println("中");
40                else if(grade < 90.0)
41                    System.out.println("良");
42                else
43                    System.out.println("优");
44                }
45                catch(MyException e)
46                {
47                    System.out.println(e.getMessage());
48                }
49         }
50         catch(MyException e)
51         {
52             System.out.println(e.getMessage());
53         }
54     }
55 }
56 public class Grade
57 {
58     public static void main(String[] args)
59     {
60         // TODO Auto-generated method stub
61         Scanner in = new Scanner(System.in);
62         GradeTest G = new GradeTest();
63         System.out.println("请输入成绩:");
64         String str = in.nextLine();
65         G.InputGrade(str);
66         in.close();
67     }
68 }        

 运行结果:

   

   

   

  

posted @ 2017-11-16 22:01  我命倾尘  阅读(179)  评论(0编辑  收藏  举报