Java课程04总结

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

运行结果:

 总结:

异常处理的目的是依据实际情况提供不同的错误应对策略与手段,使程序更稳定,更安全。 异常处理的主要用途是提供准确的错误消息,解释失败的原因、位置和错误类型等,同时提供一定的恢复能力,尽可能地保证数据完整性不被破坏,并让程序能继续运行。

 2.

 1 class AboutException {
 2    public static void main(String[] a) 
 3    {
 4       int i=1, j=0, k;
 5       k=i/j;
 6       System.out.println("整数除以零"+k);
 7 //      double d1=100,d2=0,result;
 8 //      result=d1/d2;
 9 //      System.out.println("浮点数除以零"+result);
10    }
11 }

整型除以零会抛出异常

浮点型(float、double)因为引入了无限的概念,所以不会异常。

3.

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

运行结果:

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

运行结果:

4.

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

 运行结果:

finally语句块没有执行。

原因:可以看到只打印了try代码块中的语句,finally代码块中的语句并没有打印,这是因为System.exit(0);它表示退出当前Java虚拟机,一旦退出Java虚拟机,任何代码都不会再执行。

如果当一个线程在执行 try 语句块或者 catch 语句块时被打断(interrupted)或者被终止(killed),与其相对应的 finally 语句块可能不会执行。

详细原因参考:原文链接:http://blog.csdn.net/jiasanshou/article/details/6996932

 

原文地址:https://www.cnblogs.com/janeszj/p/9931177.html