Tuesday, 22 May 2012

Runtime exceptions

In Java, RuntimeException is not checked. Runtime exceptions represent problems that are result of a programming problem, and as such, the API client code cannot reasonably be expected to recover from them or to handle them in anyway. Such problem include arithmetic exceptions, such as dividing by zero; pointer exceptions, such as trying to access an object through a null reference; and illegal arguments exceptions. It is common practice to throw RuntimeException when the user calls a method incorrectly. For example, a method can check if one of its arguments is incorrectly null. If it is, the method might throw a NullPointerException, which is an unchecked exception. For other kinds of incorrect arguments, the method can throw IllegalArgumentException.

The bottom line guideline is: 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.

Any Exception (checked) that can be thrown by a method is part of the method's public programming interface. Those who call a method must know about the exceptions that a method can throw so that they can decide what to do about them.

Reference: http://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html

No comments :

Post a Comment