Back to Study Material Java

Exception Handling in Java

An exception in Java is an event that disrupts the normal flow of a program. When an error occurs inside a method, Java creates an exception object contain...

Exception Handling in Java

Managing Errors Gracefully with Exceptions

An exception in Java is an event that disrupts the normal flow of a program. When an error occurs inside a method, Java creates an exception object containing information about the error—its type, message, and the call stack at the moment it occurred—and throws it up the call stack. If no code catches the exception before it reaches the top of the stack, the JVM terminates the current thread and prints a stack trace.

The Exception Hierarchy All exceptions in Java inherit from java.lang.Throwable. Throwable has two direct subclasses: Error and Exception. Errors (like OutOfMemoryError or StackOverflowError) represent serious problems the application cannot reasonably recover from and should almost never be caught. Exceptions are divided into checked exceptions and unchecked exceptions. Checked exceptions extend Exception (but not RuntimeException) and must be either caught or declared in the method signature with throws. Unchecked exceptions extend RuntimeException and do not require explicit handling.

Checked vs Unchecked Exceptions Checked exceptions model conditions that a well-written program should anticipate and handle—file not found, network timeout, malformed input. The compiler enforces that you address them. Unchecked exceptions usually indicate programming bugs: a NullPointerException means you dereferenced a null reference, an ArrayIndexOutOfBoundsException means you calculated an index incorrectly. These should be fixed rather than caught.

Why Exceptions Are Better Than Error Codes Before exceptions, functions returned error codes that callers had to check after every call. Forgetting to check a code meant silent bugs. Exceptions cannot be silently ignored; if not caught they propagate. They also separate error-handling logic from normal logic, making both easier to read. The call stack recorded in the exception object gives precise information about where the error originated, which is invaluable during debugging.

Code Example: // Checked exception example import java.io.*; void readFile(String path) throws IOException { FileReader fr = new FileReader(path); // throws FileNotFoundException // ... }

// Unchecked exception example int[] arr = {1, 2, 3}; try { System.out.println(arr[5]); // throws ArrayIndexOutOfBoundsException } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Index out of range: " + e.getMessage()); }

Interview Tip: Be ready to explain why catching RuntimeException broadly (catch (RuntimeException e) {}) is usually a bad practice in production code. RuntimeException covers programming mistakes that should be fixed, not swallowed. Catching them broadly masks bugs and makes the system appear to work when it is silently failing. The exception: at the topmost layer of an application (such as a web request handler), catching RuntimeException and logging it before returning an error response is acceptable because it prevents the entire server from crashing.