码迷,mamicode.com
首页 > 编程语言 > 详细

java之异常处理

时间:2018-11-11 23:20:51      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:bounds   exit   匹配   visio   log   who   异常   顺序   row   

AboutException的测试:

 1 package test;
 2 import javax.swing.*;
 3 
 4 class AboutException {
 5    public static void main(String[] a) 
 6    {
 7       @SuppressWarnings("unused")
 8     int i=1, j=0, k;
 9       //System.out.println("第一次直接对两个整数进行除法运算结果:");
10       //k=i/j;
11 
12 
13     try
14     {
15         System.out.println("第五次测试finally语句在没有报错的前提下以及提前退出语句下是否会被执行:");
16          //System.out.println("第二次将两个整数放在try{}catch{}函数中进行除法运算结果:");
17         k = i/j;    // Causes division-by-zero exception
18         //throw new Exception("Hello.Exception!");
19         //System.out.println("第三次测试finally语句在没有报错的情况下是否会被执行:");
20         
21     }
22     
23 
24     
25     catch (Exception e)
26     {
27         
28             System.out.println(e.getMessage());
29             System.exit(0);
30         
31     }
32 
33     
34     finally
35      {
36              JOptionPane.showConfirmDialog(null,"OK");
37      }
38         
39   }
40 }

 

1.第一次

技术分享图片

 

 技术分享图片

 

因为0不能作为分母而报错。

 

第二次

 技术分享图片

 

可以看到在try{}catch{}函数中虽然没有报错,但运算结果是将报错信息输出:/ by zero,

可以看出try{} catch{}函数的作用,还可以看到错误的信息。

第三次

技术分享图片

技术分享图片

 

 

 

 

可以看到,在程序没有报错的情况下,finally任然会被执行。

第四次

 技术分享图片

技术分享图片

 

 

 

可以看到,在报错的前提下,加入提前退出语句后只输出了错误信息,而没有执行finally语句。

第五次

 技术分享图片

 

可以看到,在没有报错的前提下,catch语句不能正常执行,所以提前退出语句不执行,finally语句得以正常执行。

综上所述,有

(1)Finally语句不管有没有异常发生,finally语句都得以运行,但是在遇到提前退出语句后不能被执行。

(2)try{}catch{}函数能将错误信息抛出.

ThrowDemo的测试:

 1 package test;
 2 public class ThrowDemo { 
 3     public static void main(String[] args) { 
 4 //        try {
 5             double data = 100 / 0.0;
 6             System.out.println("浮点数除以零:" + data); 
 7 //            if(String.valueOf(data).equals("Infinity"))
 8 //            { 
 9 //                System.out.println("In Here" ); 
10 //                throw new ArithmeticException("除零异常");
11 //            }
12 //        } 
13 //        catch(ArithmeticException e) { 
14 //            System.out.println(e); 
15 //        } 
16     } 
17 }
18  

1.第一次测试

技术分享图片

没有报错,但是如果将0.0改为0就会有如下结果:

技术分享图片

这个错误相比不是很陌生了,因为在上一个测试中我们遇到了无数次这个错误类型,就是因为分母为零,根据Java对double的定义运算,结果为无限。

2.第二次测试

技术分享图片

系统抛出新的错误被截取,

3.第三次测试

  技术分享图片

错误被截取后正常输出。技术分享图片

在这个测试里,第二个catch没有被编译执行。

CatchWho测试:

 1 package test;
 2 public class CatchWho { 
 3     public static void main(String[] args) { 
 4         try { //2
 5                 try { //1
 6                     throw new ArrayIndexOutOfBoundsException(); 
 7                 } 
 8                 catch(ArrayIndexOutOfBoundsException e) { 
 9                        System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch"); 
10                 }
11  
12             throw new ArithmeticException(); //扔出新的错误
13         } 
14         catch(ArithmeticException e) { 
15             System.out.println("发生ArithmeticException"); 
16         } 
17         catch(ArrayIndexOutOfBoundsException e) { //没有输出,因为前面有一个catch将错误捕捉,这个就不会被执行了
18            System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 
19         } 
20     } 
21 }

测试结果

技术分享图片

 

 抛出错误类型。

CatchWho2测试:

 1 package test;
 2 public class CatchWho2 { 
 3     public static void main(String[] args) { 
 4         try {
 5                 try { //第一次扔出错误
 6                     throw new ArrayIndexOutOfBoundsException(); 
 7                 } 
 8                 catch(ArithmeticException e) {//截取错误,但没有输出
 9                     System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 
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 }

EmbededFinally测试;

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

测试结果:

技术分享图片

 总结:

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

java之异常处理

标签:bounds   exit   匹配   visio   log   who   异常   顺序   row   

原文地址:https://www.cnblogs.com/msdog/p/9943602.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!