Sunday, April 4, 2010

Java Exception:: Quick Reference

Exception is the way in java to indicate the error condition.

All exceptions inherit from Throwable. Throwable class has 2 branches, one is Exception and another is Error. We normally catch objects of Exception class.

try/catch/finally keywords are used to handle the exception.
One try can be followed by many catch statement. That happen when piece of code can throw more than one type of Exception and every exception is subjected to be handled in different way.


Checked and Un-checked Exception is about the coding style.
If any method is throwing any exception which is sub-class of exception (RuntimeException is oddity to this) then during the compilation it is checked whether method is declared to throw exception or not.

If exception being thrown is sub-class of RuntimeException, then compile time checking is not done. (Compile time checking means check to see if method handles or throws these exceptions)


"finally" code block is the last piece of block where you want to do the processing. It is the section of the code which is executed irrespective to the occurrence of exception. You may skip this piece of section at any time. Compilation does not mandate to have this. But as part of programming practices, the piece of code which is related to freeing the resources.

The finally clause is optional. However, each try statement requires at least one catch or a finally clause.


Cause can also be associated with the exception. I have never seen doing so. And from different documentation, I found that this is more to cope up with legacy code.


References/ More to read::
  1. http://www.artima.com/designtechniques/exceptions.html
  2. Designing with exceptions
  3. http://www.java2s.com/Article/Java/Development/Breaking_Java_exceptionhandling_rules_is_easy.htm
  4. http://www.artima.com/underthehood/exceptions.html
  5. www.cs.cmu.edu/~aldrich/papers/ecoop05exnjava.pdf
  6. Good Practices for Exception based design
  7. Checked Exceptions Good-bad

No comments:

Post a Comment