標籤: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