Core Java Exception Handling Concepts
Expert View
In real-world software development, understanding the difference between throw, throws, and structured blocks like try-catch-finally is fundamental. As a software engineer, I’ve seen systems fail due to improper error flow. When these are used effectively, your code becomes reliable, predictable, and easy to maintain.
1. The try, catch, and finally Blocks
These are the backbone of Java’s exception handling mechanism:
- try — encloses code that might throw exceptions.
- catch — handles exceptions of specific types.
- finally — executes cleanup code, whether an exception occurs or not.
public class TryCatchExample {
public static void main(String[] args) {
try {
int num = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("Finally always executes!");
}
}
}
2. The throw and throws Keywords
throw is used to manually raise an exception, while throws declares that a method may pass exceptions to its caller.
class ThrowsExample {
static void validateAge(int age) throws ArithmeticException {
if(age < 18)
throw new ArithmeticException("Underage! Not eligible.");
else
System.out.println("Access granted!");
}
public static void main(String[] args) {
try {
validateAge(16);
} catch (ArithmeticException e) {
System.out.println("Caught: " + e.getMessage());
}
}
}
3. Multiple Catch Clauses
Java allows multiple catch blocks to handle different exception types separately. Each block should handle a specific scenario.
public class MultiCatch {
public static void main(String[] args) {
try {
int[] arr = {1, 2, 3};
System.out.println(arr[5]);
int x = 10 / 0;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Array index out of range!");
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!");
} catch (Exception e) {
System.out.println("General Exception caught!");
}
}
}
4. The Throwable Class
All exceptions in Java derive from the Throwable class, which provides useful methods:
getMessage() – returns the error message.
printStackTrace() – prints the execution trace.
toString() – returns exception class and message.
public class ThrowableDemo {
public static void main(String[] args) {
try {
int res = 5 / 0;
} catch (Exception e) {
System.out.println("Message: " + e.getMessage());
e.printStackTrace();
}
}
}
5. Unchecked Exceptions
Unchecked exceptions (RuntimeExceptions) occur due to logic errors during execution and are not verified at compile time.
class UncheckedDemo {
public static void main(String[] args) {
String str = null;
System.out.println(str.length()); // Causes NullPointerException
}
}
6. Checked Exceptions
Checked exceptions occur due to external factors and must be either handled or declared using throws.
import java.io.*;
class CheckedDemo {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("data.txt");
} catch (IOException e) {
System.out.println("Checked Exception: " + e.getMessage());
}
}
}
🔑 Key Summary
try-catch-finally ensures controlled handling of runtime errors.
throw raises exceptions manually, while throws declares them.
- Multiple catch blocks allow handling of various exceptions individually.
- All exception classes stem from
Throwable.
- Unchecked exceptions indicate coding errors, while checked ones relate to external operations.
- Mastering these makes your Java code clean, predictable, and robust.