2010年10月10日 星期日

Java Exceptions (interview 問到囉!!!)

For run time exceptions

  • method signature does not need to declare runtime exceptions
  • caller to a method that throws a runtime exception is not forced to catch the runtime exception
  • Runtime exceptions extend from RuntimeException or Error
Example:
ArithmeticException, IllegalArgumentException, ClassCastException, NullPointerException, IndexOutOfBoundException,
SystemException, NoSuchMethodException.
For checked exceptions:
  • method must declare each checked exception it throws
  • caller to a method that throws a checked exception must either catch the exception or throw the exception itself
  • Checked exceptions extend from Exception

Checked exceptions indicate an exceptional condition from which a caller can conceivably recover. Runtime exceptions indicate a programmatic error from which a caller cannot normally recover.

Checked exceptions force you to catch the exception and to do something about it. You should always catch a checked exception once you reach a point where your code can make a meaningful attempt at recovery. However, it is best not to catch runtime exceptions. Instead, you should allow runtime exceptions to bubble up to where you can see them.


沒有留言:

張貼留言