Java
Exceptions
Technically, an exception is any class that extends the java.lang.Exception class. More generally, an exception is thrown whenever anything unexpected happens during the execution of a program. It is the programmer’s responsibility to make sure that a program recovers gracefully from an exception.
Exceptions and Errors: The java.lang.Throwable class has two standard subclasses: Exception and Error.Errors usually represent unexpected behavior that the program cannot possibly recover from, so errors are usually ignored by the programmer. Exceptions are problems from which the program can (and should) recover, and they are generally caught and handled within a program.
Checked and unchecked exceptions: A RuntimeException (java.lang.RuntimeException) or a descendant of RuntimeException is considered an unchecked exception, meaning that, as it occurs at runtime, the compiler cannot determine whether the exception might be thrown. Java will allow a program to throw unchecked exceptions without specifically declaring that it might do so. All other exceptions are checked exceptions, meaning that the compiler will check for them. Any method that throws a checked exception must either catch the exception or declare the exception in its method header. A class whose methods throw checked exceptions must also declare the exceptions that it can throw.
Exception Handling
When an exception occurs, all execution in the program (more precisely, in the thread) halts and control is transferred to an exception handling mechanism. At this point, one of two things will happen: the exception can be caught, or the exception can be allowed to propagate to another part of the program.
try/catch/finally: Exception handling is done through the try and catch statements. Code that might generate an exception is enclosed in a try block, a pair of braces preceded by the keyword try. Each try block is immediately followed by one or more catch blocks, code in between a pair of braces preceded by the keyword catch and the type and name of the Exception in parentheses. Whenever code in the try block throws an exception of the above type, control is transferred to the corresponding catch block.
-
try {
exception generating code here
} catch (Exception1 e1) { exception handling code
>here
} catch (Exception2 e2) { exception handling
code here
} ...
After the catch block executes, control is transferred back to the calling method.
A finally block can follow any catch block(s). Before transferring control to the calling method, the code in the finally block executes regardless of whether exceptions were thrown.
-
try {
exception generating code here }
catch (Exception1 e1) { exception handling code here }
finally {
clean-up code here }
Propagating exceptions: Any exception that is not caught in the method that throws it must be declared in the method header with the throws keyword:
Example:
-
public void findCar(Car c) throws NoCarException, MyException
An exception that is not caught in the method that throws it is propagated to the calling method, where it can be handled by a try/catch statement in that method. If it is not handled in the calling method, the exception is propagated through the method call stack until it reaches the main method. At this point, if the exception is not handled, the program terminates and control is returned to the shell. It is generally considered poor programming practice to let a program terminate this way. All exceptions should be caught before they propagate out of the main method.
Creating exceptions: Since all exceptions are descendants of java.lang. Exception, it is possible to create program-specific exceptions by creating a new class that extends Exception or one of its subclasses.
Example:
-
public class my Exception extends Exception { ... }
The standard constructor for a new exception consists of only one line invoking the superclass constructor. One of the superclass constructors takes a user-defined String as a parameter to facilitate error messages. An additional field can be added in the subclass to store data obtained from the String.
Example:
-
public class myException extends Exception {
String data;
public myException (String someData) {
super("Exception involving " + someData);
data = someData;
}
}
The default toString() method of Exception (inherited from Object) prints the String from the constructor (which may be null) concatenated with a String including the method and line number that threw the exception.
Exceptions

