學習 learning Java for android-Exceptions

來源:互聯網
上載者:User

標籤:android   style   io   ar   os   使用   java   sp   for   

  Exceptions 

 java允許我們建立自己的異常類,但是在建立之前先問問自己是不是jdk內建的異常類真的不能滿足自己的需要。如果有的話,我們應該使用jdk內建的異常類。因為當其他人閱讀的代碼時,他們通常是熟悉了jdk的異常系統,對你寫的異常類會很陌生。如果需要自己建立異常類的話,我們要考慮我們寫的異常類是應該繼承Exception還是RuntimeException。一般來說都是繼承後者。

  異常處理的建議:

  There are a few additional items to keep in mind when working with throws clauses and throw statements:

  1.You can append a throws clause to a constructor and throw an exception from the constructor when something goes wrong while the constructor is executing. The resulting object will not be created.
   2.When an exception is thrown out of an application’s main() method,the virtual machine terminates the application and calls the exception’s printStackTrace() method to print, to the console, the sequence of  nested method calls that was awaiting completion when the exception was thrown.
  3.If a superclass method declares a throws clause, the overriding subclass method does not have to declare a throws clause. However,if it does declare a throws clause, the clause must not include the names of exception classes that are not also included in the superclass method’s throws clause.
  4.A checked exception class name does not need to appear in a throws clause when the name of its superclass appears.
  5.The compiler reports an error when a method throws a checked exception and does not also handle the exception or list the exception in its throws clause.
  6.Do not include the names of unchecked exception classes in a throws clause. These names are not required because such exceptions should never occur. Furthermore, they only clutter source code,and possibly confuse someone who is trying to understand that code.
  7.You can declare a checked exception class name in a method’s throws clause without throwing an instance of this class from the method. Perhaps the method has yet to be fully coded.
對於第二點,小弟理解的不是很到位,有理解的大哥還麻煩指點下

學習 learning Java for android-Exceptions

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.