1. Exception Handling
An exception is an unexpected unwanted event that disturbs the normal flow of a program.
Examples
Tyre puncture, sleeping exception, file-not-found exception.
Main Objective
Provide an alternate way to continue the rest of the program normally and achieve graceful termination.
try {
// Read data from London file
} catch (FileNotFoundException ex) {
// Use local file and continue normally
}
2. Runtime Stack Mechanism
For every thread, the JVM creates a runtime stack. Every method call made by that thread is stored in the corresponding stack.
- Each entry is called a Stack Frame or Activation Record.
- After a method completes, its stack-frame entry is removed.
- After all method calls complete, the stack becomes empty and is destroyed by the JVM before the thread terminates.
main()
└── doStuff()
└── doMoreStuff()
3. Default Exception Handling in Java
When an exception occurs inside a method, the method creates an exception object containing the exception name, description and location/stack trace, then hands the object to the JVM.
- The JVM checks whether the current method contains exception-handling code.
- If not, the method terminates abnormally and its stack frame is removed.
- The JVM checks the caller method and continues this process up to
main(). - If no handler is found, the JVM passes responsibility to the Default Exception Handler.
- The default handler prints exception information and terminates the program abnormally.
Exception in thread "main"
ExceptionName: description
at ClassName.method(ClassName.java:line)
at ClassName.main(ClassName.java:line)
4. Exception Hierarchy
The Throwable class acts as the root of the Java exception hierarchy and defines two major child classes: Exception and Error.
Exception
Most exceptions are caused by the program and are recoverable. For example, a missing remote file can be handled by using a local file.
Error
Errors are generally associated with lack of system resources and are not normally recoverable by application code, such as OutOfMemoryError.
Throwable ├── Exception │ └── RuntimeException └── Error
Examples Mentioned in the Source
| Category | Examples |
|---|---|
| Runtime exceptions | ArithmeticException, NullPointerException, ClassCastException, ArrayIndexOutOfBoundsException, StringIndexOutOfBoundsException, IllegalArgumentException, NumberFormatException |
| Errors | VM Error, StackOverflowError, OutOfMemoryError, AssertionError, ExceptionInInitializerError |
| I/O exceptions | EOFException, FileNotFoundException |
5. Checked and Unchecked Exceptions
The source describes checked exceptions as exceptions checked by the compiler for smooth runtime execution. If a checked exception can be raised, it must be handled using try/catch or declared with throws; otherwise a compile-time error occurs.
Unchecked exceptions are not checked by the compiler for handling. Examples include ArithmeticException, NullPointerException, ClassCastException, array/string index exceptions, IllegalArgumentException and NumberFormatException.
| Type | Examples from the source |
|---|---|
| Checked | IOException, InterruptedException, FileNotFoundException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException |
| Unchecked | RuntimeException and its child classes; Error and its child classes |
Fully Checked vs Partially Checked
| Type | Meaning | Examples |
|---|---|---|
| Fully checked | All child classes are also checked. | IOException, InterruptedException |
| Partially checked | Some child classes are unchecked. | Exception, Throwable |
6. Exception Handlers: try and catch
The source calls code that may raise an exception risky code. Risky code is placed inside a try block, while corresponding handling code is placed inside a catch block.
try {
// risky code
} catch (Exception ex) {
// handle code
}
7. Control Flow in try/catch
Case Studies
| Situation | Execution | Result |
|---|---|---|
| No exception | 1 → 2 → 3 → 5 | Normal termination |
| Exception at statement 2 and catch matches | 1 → 4 → 5 | Normal termination |
| Exception at statement 2 and catch does not match | 1 | Abnormal termination |
| Exception at statement 1 and catch does not match | Abnormal termination | Abnormal termination |
| Exception at statement 5 | 1 → 2 → 3 | Abnormal termination |
- Once an exception occurs anywhere in a try block, the remaining statements in that try block are not executed, even if the exception is handled.
- An exception can also occur inside a catch or finally block.
- An exception raised outside a try block always causes abnormal termination unless handled elsewhere by normal propagation.
8. Printing Exception Information
| Method | Printable information |
|---|---|
printStackTrace() | Exception name, description and stack trace |
toString() | Exception name and description |
getMessage() | Description/message |
Throwable
Provides printStackTrace(), toString() and getMessage().
9. Multiple catch Blocks
The source recommends a separate catch block for each exception type when the handling differs between exception types.
try {
BufferedReader br =
new BufferedReader(new FileReader("abc.txt"));
} catch (ArithmeticException ex) {
// handling code
} catch (FileNotFoundException ex) {
// handling code
} catch (NullPointerException ex) {
// handling code
} catch (Exception ex) {
// handling code
}
10. finally Block
The finally block is the recommended place for cleanup code that should execute regardless of whether an exception is raised or handled.
- Cleanup code should not depend on the try block completing every statement.
- Cleanup code should not be placed only in catch because catch does not execute when there is no exception.
- The finally block normally executes whether an exception occurs or not, and whether it is handled or not.
- If a return statement occurs in try/catch, finally executes before the return is completed.
try {
// risky code
} catch (Exception ex) {
// handle code
} finally {
// cleanup code
}
System.exit(0) shuts down the JVM, so finally is not executed in that case.11. throw and throws
throw
The throw keyword is used to explicitly create/hand over an exception object to the JVM. The source highlights user-defined/custom exceptions as an important use case.
throw new ArithmeticException("/ by zero");
- After a throw statement, statements written immediately after it are unreachable.
- The thrown object must be a Throwable type.
- Throwing a null reference results in a NullPointerException.
throws
The throws keyword delegates exception-handling responsibility to the caller.
public void readFile() throws FileNotFoundException {
// code that may raise the exception
}
| throw | throws |
|---|---|
| Used to explicitly throw an exception object. | Used to declare/delegate exception-handling responsibility. |
| Used inside method/block logic. | Used in a method or constructor declaration. |
| Works with Throwable objects. | Declares Throwable types. |
12. Top Exceptions and Who Raises Them
The source divides exceptions/errors based on who raises them into JVM Exceptions and Programmatic Exceptions.
| Exception / Error | Raised by | Source description |
|---|---|---|
| AIOB | JVM | Array index is outside the valid range. |
| NPE | JVM | An operation is performed on null. |
| CCE | JVM | Invalid casting from parent/object type to child type. |
| ArithmeticException | JVM | Raised automatically for arithmetic problems such as division by zero. |
| StackOverflowError | JVM | Can occur during excessive recursive method calls. |
| NoClassDefFoundError | JVM | Class definition cannot be found at runtime. |
| ExceptionInInitializerError | JVM | Occurs while executing static initialization. |
| IllegalArgumentException | Programmer/API | Method invoked with an illegal argument. |
| NumberFormatException | Programmer/API | String-to-number conversion is attempted with an improperly formatted string. |
| IllegalStateException | Programmer/API | Method is invoked at an inappropriate time/state. |
| AssertionError | Programmer/API | An assert statement fails. |
13. Java 7 Exception Handling Enhancements
Try-with-resources
Resources opened in the try block are closed automatically when control reaches the end of the try block, normally or abnormally.
Multi-catch
A single catch block can handle multiple different exception types.
Try-with-resources
- Reduces the need for explicit cleanup and reduces code length.
- Multiple resources can be declared using semicolons.
- Resources must be auto-closable.
- A resource is auto-closable when its class implements
java.lang.AutoCloseable. - I/O, database and network-related resources commonly implement this interface.
AutoCloseablewas introduced in Java 7 and containsclose().- Resource reference variables are implicitly final within the try block, so reassignment is not allowed.
- From Java 7, try-with-resources can be used without an explicit catch/finally block.
try (R1 r1 = ...; R2 r2 = ...; R3 r3 = ...) {
// use resources
}
Multi-catch
try {
// risky code
} catch (IOException | SQLException ex) {
// common handling code
}
14. Exception Propagation and Re-throwing
Exception Propagation
If an exception is raised inside a method and is not handled there, the exception object is propagated to the caller method. The caller becomes responsible for handling it.
Re-throwing Exception
The source describes re-throwing as an approach that can be used to convert one exception into another exception type.
15. User-Defined Exceptions
If a programmer implements their own exception, it is called a user-defined exception.
The source states that a user-defined exception class should extend one of the following:
ExceptionRuntimeExceptionThrowable
class MyException extends Exception {
public MyException(String message) {
super(message);
}
}
16. Examples from the Source PDF
The later pages of the PDF contain practical exception examples, program output and exception traces. These pages are preserved below as page images so the original code screenshots, console output and diagrams remain available.
ClassCastException stack trace showing an invalid cast from java.lang.Object to java.lang.String.17. Quick Revision
| Topic | Key point |
|---|---|
| Exception | Unexpected event that disturbs normal program flow. |
| Throwable | Root class of the exception hierarchy. |
| try | Contains risky code. |
| catch | Contains exception-handling code. |
| finally | Used mainly for cleanup code. |
| throw | Explicitly throws an exception object. |
| throws | Declares/delegates exception-handling responsibility. |
| Checked exception | Compiler requires handling or declaration. |
| Unchecked exception | Compiler does not require explicit handling. |
| Try-with-resources | Automatically closes AutoCloseable resources. |
| Multi-catch | One catch block handles multiple unrelated exception types. |
| Propagation | Unhandled exception moves to the caller. |
| User-defined exception | Custom exception implemented by the programmer. |