Buy Reviews
Powered by MaxBlogPress  

Java – 常見的例外狀況

點閱人數:13次 二月 9th, 2010 by ㄚ琪 |

Java物件導向程式設計與SCJP認證明解

這一本書是這陣子在看的,作者很貼心的準備了SCJP總複習擬真試題給我們免費下載,真好!

我看到12-3-2 常見的例外狀況,有關於編譯器不會檢核的RuntimeException的子孫類別有:

NullPointerException
IllegalArgumentException
NumberFormatException
ClassCastException
ArrayIndexOutOfBound:使用索引值存取陣列元素時,索引值超出陣列範圍。
NegativeArraySizeException
ClassCastException
SecurityException
ArithmeticException

應檢核例外:

java.text.ParseException
java.io.FileNotFoundException
java.io.IOException
java.sql.SQLException
java.lang.InterruptedException:使用索引值存取陣列元素時,索引值超出陣列範圍。
java.lang.ClassNotFoundException

有沒有看到ArrayIndexOutOfBound跟java.lang.InterruptedException這兩個解釋一模一樣?怎會這樣?對初學者來說不知道能不能分辨出來,還好ㄚ琪看了很多本SCJP了,所以一會兒就看到這個錯誤了,可能是作者那時在顧小孩的時候,貼錯了吧!

java.lang.InterruptedException主要是執行緒非執行中而嘗試中斷所引發的例外處理,在執行緒那章會常常看到,原文手冊是這樣寫:

『Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity. Occasionally a method may wish to test whether the current thread has been interrupted, and if so, to immediately throw this exception. The following code can be used to achieve this effect:

  if (Thread.interrupted())  // Clears interrupted status!
      throw new InterruptedException();

希望大家可以注意到,不會搞錯!

喜歡這篇文章嗎?請幫這篇加入共用書籤,謝謝:
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Hemidemi
  • MyShare
  • Technorati
  • StumbleUpon
  • Diigo
  • Facebook
  • Twitter

Tags: , , , ,

發表迴響

名稱:(必)
電子郵件(並不會被發佈)(必)
個人網站

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>