学习 learning Java for android-Exceptions

  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.
对于第二点,小弟理解的不是很到位,有理解的大哥还麻烦指点下

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。