首页 > 技术文章 > 【已解决】Java异常处理

rainbow-1 2020-10-27 23:03 原文

 

 

 

 

 try 语句块:放置所有可能发生错误的代码

catch语句块:处理错误

finally语句块:不管是否有异常,始终会执行


 


 

 #为什么浮点数除以0不会崩溃#

因为java的float和double使用了 IEEE 754 标准。

这个标准规定:浮点数除以0等于正无穷或负无穷。

infinity单词的意思是:无穷大

NaN是 N ot a N umber的简称,也就是非数。

于是,我们发现, 正无穷大 的定义居然是 1.0f/0.0f 。 负无穷大 的定义为**-1.0f/0.0f**, 非数 的定义为 0.0f/0.0f


 

#异常的多态特性#

 

 

 

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

 

 

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

 

 

 

catch (Exception e)
{

System.out.println("Level 3:" + e.getClass().toString());

}

 

 

推荐阅读