码迷,mamicode.com
首页 > 移动开发 > 详细

学习 learning Java for android-Exceptions

时间:2014-11-11 16:23:21      阅读:219      评论:0      收藏:0      [点我收藏+]

标签: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

标签:android   style   io   ar   os   使用   java   sp   for   

原文地址:http://www.cnblogs.com/lucian/p/4089527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!