学习 learning Java for android-Exceptions_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 学习 learning Java for android-Exceptions

学习 learning Java for android-Exceptions

 2014/11/12 10:32:23  疯狂小孩  程序员俱乐部  我要评论(0)
  • 摘要:Exceptionsjava允许我们创建自己的异常类,但是在创建之前先问问自己是不是jdk自带的异常类真的不能满足自己的需要。如果有的话,我们应该使用jdk自带的异常类。因为当其他人阅读的代码时,他们通常是熟悉了jdk的异常系统,对你写的异常类会很陌生。如果需要自己新建异常类的话,我们要考虑我们写的异常类是应该继承Exception还是RuntimeException。一般来说都是继承后者。异常处理的建议
  • 标签:android for 学习 Java

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

上一篇: iPhone 6 Plus需求超预期,1/3新用户来自三星 下一篇: 没有下一篇了!
发表评论
用户名: 匿名