Thursday, June 30, 2005

Java - Unchecked Exceptions

There are two types of Exceptions in Java - The Checked and the Unchecked. Ever wondered why we have this classification? Any exception that can be thrown by a method is part of the method’s API and forms an integral part of that method’s declaration as are its parameters and return value. Just imagine the complexity of a method declaration if it would make all exceptions necessary to be declared! And worse, some of these exceptions would never occur in a program if the program was written carefully and diligently.

Unchecked exceptions, on the other hand, represent problems resulting from a programming problem, from which the application cannot recover or which cannot be handled in any way - for eg ArrayIndexOutOfBounds or NullPointerException.

Moral of the story - If the code can reasonably be expected to recover from an exception, make it a checked exception. If the code cannot do anything to recover from the exception, make it an unchecked exception.

Oh yes, on a related note - Are there pointers in Java? If your answer is NO, could you please explain why there are NullPointerExceptions everytime I run my code?!

No comments: