Tuesday, March 23, 2010

Java Exceptions

Exception Objects
Throwable
'-> Error
'-> AnnotationFormatError
'-> AssertionError
'-> IOError
'-> ThreadDeath
'-> ...
'-> Exception
'-> RuntimeException
'-> ArithmeticException
'-> ArrayStoreException
'-> ClassCastException
'-> ConcurrentModificationException
'-> IndexOutOfBoundsException
'-> NullPointerException
'-> ...
'-> ClassNotFoundException
'-> CloneNotSupportedException
'-> InterruptedException
'-> IOException
'-> NoSuchMethodException
'-> ...

Kinds of Exceptions
  1. checked - all exceptions that are not RuntimeException
  2. unchecked
    - Error
    - RuntimeException


Notes
- finally is always executed even if there is a change of control flow like when there is a return statement in try
- can you catch runtime exception? yes, you can catch any Throwable type but since the cost of checking for runtime exceptions exceeds the benefit of catching or specifying them, the compiler does not require that you catch them
- for readable code, it's good practice to append the string Exception to the names of all classes that inherit (directly or indirectly) from the Exception class
- what to use when creating your own exception: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

Ref: http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html

No comments:

Post a Comment