JAVA 異常 throwable exception error throws throw


1.如何理解Exception,Error和Throwable 
    Throwable是Exception和Error的父類. 
    Error表示錯誤,一般是系統級的錯誤! 
     Exception一般是程序運行期間的錯誤! 
    
    通常在使用  try{}catch(Exception e){} 這種結構的時候,只能找到一半的錯誤,也就是說只能捕獲Exception范圍內的異常 並處理使得程序能夠正常運行.而Error范圍內的錯誤就無法捕獲並處理. 

   通過 try{}catch(Throwable a){} 的方式能夠處理,但是一般情況下不這樣做。 
   原因在於查看Error下面的子類,VirtualMachineError,ThreadDeath,LinkageError,從名字上看出來這些錯誤都是非常非常嚴重的,到底是否需要去捕獲或者處理呢? 
   
    Error的產生一般是JVM或者是操作系統的問題,JAVA 文檔中對Error的說明是:"Error是Throwable的子類,它的出現說明出現了嚴重的問題。一般應用程序除非有理由,否則不應該捕捉Error,通常這是非常反常的情況". 

    Exception的產生主要是在程序運行期間發生的一些不正常事件中止了程序的運行,可以通過JAVA異常處理機制捕獲異常並處理,使得程序正常運行下去。這些異常(不正常事件)有別於Error錯誤,它們通常是可修復的,程序員可以處理的。 

2.運行時異常和受檢查異常 
    1)運行時異常,屬於RuntimeException類及子類范圍的類(以及衍生類)都屬於運行時異常。 
     2)受檢查異常,在Exception范圍內,除了運行時異常的類都是受檢查異常類,為checked exception 
    3)它們之間的區別在於: 例如在代碼中寫了 throw new Exception(); 和 throw new RuntimeException(); 
  
    兩者都會在運行期間拋出異常! 
     但是在編譯階段前者的屬於拋出一個受檢查異常,要求對它進行顯式的try..catch 捕獲處理或者向上一層方法拋出,否則在編譯期間就顯示錯誤! 
     后者拋出是運行時異常,在編譯階段不予檢查,語法上不會顯示任何錯誤! 
     所以簡單的通過throw手動拋出受檢查異常 和拋出運行時異常,前者要求顯式處理,后者不要求作出處理。 

3.throw 和throws的區別 

    1)throw 是手動拋出異常,throw new **Exception(); 拋出的是某一個異常類型的實例. 
    2)throws 是方法拋出異常,寫在方法聲明處 public void show()throws **Exception,**Exception{} 緊跟throws后的是異常類型,而非異常實例,且可以聲明拋出多個異常,同時這些異常類型大多都為 受檢查異常類型。 
     3)throw 是程序員手動拋出異常,一般可用在某種流程控制,需要顯示操作失誤情況下可對外拋出異常,進入catch代碼塊,明示操作有誤等。 
     throws 方法拋出異常,通常是告知調用此方法者,本方法有可能拋出一個異常,在調用時應當要進行異常監控。且因為throws方法拋出異常為受檢查異常類型,這樣就從語法上要求更需要對受檢查異常類型作出捕獲,或者再次向上拋出。 

     例如: JDBC中 public static Class forName(String className)throws ClassNotFoundException 加載驅動類時,調用此方法. 

    因為方法聲明出 throws ClassNotFoundException 就告訴了方法調用者 本方法有可能會發生 ClassNotFoundException 異常,因為需要在調用此方法時格外注意。且此異常類型是受檢查類型,那么在編譯階段就更要求用戶調用時必須加上 try..catch(){}語句塊,或者再次往上方法聲明拋出 ClassNotFoundException 交由上層方法聲明拋出 。 

     throws 方法拋出異常的兩種處理方式 

Java代碼   收藏代碼
  1. (1)try{  
  2.         Class.forName("com.microsoft.....");  
  3.     }catch(ClassNotFoundExceptioin e){  
  4.         ...  
  5.     }  
  6.       
  7.     (2)  
  8.         public Connection getConnection()throws ClassNotFoundException{  
  9.             Class.forName("com.microsoft....");  
  10.         }  



    如果把ClassNotFoundException 改為RuntimeException 就無需處理,那也就失去了方法拋出異常的意義了。 

4.常見的運行時異常 
  ArithmaticExceptionDemo.java 
 

Java代碼   收藏代碼
  1. /** 
  2.  * 當除數為 0 時的異常  是運行時異常 編譯時並不報錯 只會在運行時發生 
  3.  * @author Simon Lv 
  4.  * 
  5.  */  
  6. public class ArithmaticExceptionDemo {  
  7.     //計算兩個數相除的結果  
  8.     public int divide(int a,int b){  
  9.             return a/b;  
  10.         }  
  11.       
  12.     //拋出異常  
  13.     //Exception in thread "main" java.lang.ArithmeticException: / by zero  
  14.     public static void main(String args[]){  
  15.             ArithmaticExceptionDemo excp = new ArithmaticExceptionDemo();  
  16.             excp.divide(10,0);    
  17.         }  
  18. }  



ArrayStoreExceptionDemo.java 

Java代碼   收藏代碼
  1. /** 
  2.  * 演示 試圖將一個錯誤的對象 存儲到一個數組時發生的異常  ArrayStoreException 
  3.  * @author Simon Lv 
  4.  *  
  5.  */  
  6. public class ArrayStoreExceptionDemo {  
  7.       
  8.     //程序的入口  
  9.     public static void main(String arsg[]){  
  10.             //Object類型 字符串數組  
  11.             Object x[] = new String[3];   
  12.               
  13.             //為了不讓程序在編譯時就檢查報錯 就故意寫成上述類型  
  14.             //如果寫成 下面類型就會直接報錯   
  15.             //String x [] = new String[3];  
  16.               
  17.             try{  
  18.                 //錯誤的類型 java.lang.ArrayStoreException: java.lang.Integer  
  19.                  x[0] = new Integer(0);           
  20.             }catch(ArrayStoreException ae){  
  21.                     System.err.println(ae.toString()+"ok");  
  22.                     ae.printStackTrace();  
  23.             }  
  24.               
  25.             System.out.println("程序正常運行!");  
  26.     }  
  27.   
  28. }  



ClassCastExceptionDemo.java 

Java代碼   收藏代碼
  1. /** 
  2.  * 類 類型裝換異常 當試圖將對象強制轉換為不是實例的子類時,拋出該異常 ClassCastException 
  3.  * @author Simon Lv 
  4.  * 
  5.  */  
  6. public class ClassCastExceptionDemo {  
  7.       
  8.     public static void main(String args[]){  
  9.               
  10.             Object x = new Integer(12);  
  11.           
  12.             try{  
  13.                 System.out.println((String)x);  
  14.             }catch(ClassCastException ce){  
  15.                     System.err.println(ce.toString());  
  16.                 }  
  17.     }  
  18.   
  19. }  



EmptyStackExceptionDemo.java 

Java代碼   收藏代碼
  1. import java.util.EmptyStackException;  
  2. import java.util.Stack;  
  3.   
  4. /** 
  5.  * 演示堆棧為空的異常 EmptyStackException 
  6.  * @author Simon Lv 
  7.  * 
  8.  */  
  9. public class EmptyStackExceptionDemo {  
  10.       
  11.     public static void main(String args[]){  
  12.           
  13.         Stack<String> s = new Stack<String>(); //棧   
  14.         s.push("a");    //先壓入 a          壓棧 push()方法  
  15.         s.push("b");    //    b  
  16.         s.push("c");    // 最后壓入 c  
  17.           
  18.         try{  
  19.             while(s.size()>0){  
  20.                 System.out.println( s.pop());    //依次循環將棧中的元素彈出 pop()方法  
  21.             }  
  22.             s.pop(); // 此動作將造成異常,因為棧中所有元素在上面的 循環中就已經彈出,為空棧  
  23.         }catch(EmptyStackException ee){  
  24.                 System.err.println(ee.toString());  
  25.                 ee.printStackTrace();  
  26.             }  
  27.     }  
  28. }  



IndexOutOfBoundsExceptionDemo.java 

Java代碼   收藏代碼
  1. /** 
  2.  * 演示 IndexOutOfBoundsException 異常   
  3.  * 指某排序索引(例如對數組、字符串或向量的排序)超出范圍時拋出。 
  4.  * @author Simon Lv 
  5.  * 
  6.  */  
  7. public class IndexOutOfBoundsExceptionDemo {  
  8.       
  9.     public static void main(String args[]){       
  10.             int num [] = new int[10];  
  11.             try{  
  12.                 //數組10個長度   12次循環就報錯  
  13.                 for(int i=0;i<12;i++){  
  14.                     System.out.println(num[i]); //循環超出范圍  
  15.                 }  
  16.             }catch(IndexOutOfBoundsException ie){  
  17.                     System.err.println(ie.toString());  
  18.                     ie.printStackTrace();  
  19.                     System.out.println( ie.getCause());  
  20.                     System.err.println(ie.getMessage());  
  21.                       
  22.                 }  
  23.                   
  24.             System.out.println("程序還 能繼續執行");  
  25.         }  
  26. }  



NegativeArraySizeExceptionDemo.java 

Java代碼   收藏代碼
  1. /** 
  2.  * 演示 NegativeArraySizeException  異常 
  3.  *  如果應用程序試圖創建大小為負的數組,則拋出該異常。 
  4.  * @author Simon Lv 
  5.  * 
  6.  */  
  7. public class NegativeArraySizeExceptionDemo {  
  8.       
  9.     public static void main(String args[]){  
  10.               
  11.             try{  
  12.                 int num [] = new int [-9];   //創建大小為負的數組,則拋出該異常。  
  13.                 System.out.println(num[0]);  
  14.             }catch(NegativeArraySizeException ne){  
  15.                 System.err.println(ne.toString()); //err紅色打印  
  16.                 ne.printStackTrace();  
  17.             }  
  18.         }  
  19. }  



NullPointerExceptionDemo.java 

Java代碼   收藏代碼
  1. /** 
  2.  * 當應用程序試圖在需要對象的地方使用 null 時,拋出該異常。這種情況包括:  
  3.     調用 null 對象的實例方法。 
  4.     訪問或修改 null 對象的字段。 
  5.     將 null 作為一個數組,獲得其長度  
  6.     將 null 作為一個數組,訪問或修改 
  7.     將 null 作為 Throwable 值拋出 
  8.     應用程序應該拋出該類的實例,指示其他對 null 對象的非法使用 
  9.  
  10.  * @author Simon Lv 
  11.  * 
  12.  */  
  13. public class NullPointerExceptionDemo {  
  14.       
  15.     String nameString;  //類字符串成員變量  默認為null  
  16.       
  17.     public static void main(String args[]){  
  18.               
  19.             try{  
  20.                 //返回字符串第一個字符 但將出現異常 相當於 null.charAt(0);  
  21.                 char c = new NullPointerExceptionDemo().nameString.charAt(0);  
  22.                   
  23.                 System.out.println(c);  
  24.             }catch(NullPointerException ne){  
  25.                 System.err.println(ne.toString());  
  26.                 ne.printStackTrace();  
  27.             }  
  28.         }  
  29.   
  30. }  



NumberFormatExceptionDemo.java 

Java代碼   收藏代碼
  1. /** 
  2.  * 演示格式轉換異常 NumberFormatException    
  3.  * 當應用程序試圖將字符串轉換成一種數值類型,但該字符串不能轉換為適當格式時,拋出該異常 
  4.  * @author Simon Lv 
  5.  * 
  6.  */  
  7. public class NumberFormatExceptionDemo {  
  8.       
  9.     public static void main(String args[]){  
  10.               
  11.             String a = "3";  
  12.             int b = (int)new Integer(a);  
  13.             System.out.println(b);//這個是沒有問題的  
  14.               
  15.             try{  
  16.                 String c = "aa";  
  17.                 //java.lang.NumberFormatException: For input string: "aa"  
  18.                 int d = (int)new Integer(c);  
  19.               
  20.                 System.out.println(d);//這個是有問題的  
  21.             }catch(NumberFormatException ne){  
  22.                 System.err.println(ne.toString());  
  23.                 ne.printStackTrace();  
  24.             }  
  25.         }  
  26. }  


 轉載:http://lvp.iteye.com/blog/356650


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM