How to Push to a GitHub Repository in IntelliJ
Throwable class.Understanding this hierarchy gives you several advantages:
In this article, we’ll visualize the Java exception hierarchy, explain each layer, and cover practical exception handling strategies and best practices you’ll use every day.
ThrowableAt the very top is the Throwable class, which represents anything that can be thrown as an error or exception.
It has two main direct subclasses:
Error: Serious problems at the JVM or system level (not recoverable)Exception: Application-level issues (recoverable)Error – System/JVM-Level Critical FailuresError represents severe problems that applications should not try to handle—these are typically related to the JVM or hardware.
| Class | Description |
|---|---|
StackOverflowError |
Thrown when the stack overflows (e.g., infinite recursion) |
OutOfMemoryError |
Thrown when the JVM runs out of memory |
VirtualMachineError |
Indicates internal JVM errors (e.g., JIT failures) |
Tip: Never try to catch or handle Error in your code—let the JVM take care of it.
Exception – Recoverable Application-Level IssuesException covers problems that can occur during normal program execution and that your application might want to catch and recover from.
| Class | Description |
|---|---|
IOException |
I/O issues (e.g., file not found, network problems) |
SQLException |
Problems encountered during database access |
ClassNotFoundException |
Thrown when a class can’t be found at runtime |
RuntimeException |
Unchecked exceptions, usually programming mistakes |
throws or handled with try-catchtry {
BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
} catch (IOException e) {
System.out.println("Unable to read the file.");
}
int[] arr = {1, 2, 3};
System.out.println(arr[5]); // ArrayIndexOutOfBoundsException
RuntimeException| Exception Class | Description |
|---|---|
ArithmeticException |
Arithmetic errors (e.g., division by zero) |
NullPointerException |
Accessing a method or field on null |
NumberFormatException |
Failing to parse a string as a number |
IndexOutOfBoundsException |
Accessing an array/list index out of bounds |
IndexOutOfBoundsException has important subclasses:
ArrayIndexOutOfBoundsExceptionStringIndexOutOfBoundsExceptionYou can define your own exceptions to fit your business logic.
// Example of a checked exception
public class InsufficientBalanceException extends Exception {
public InsufficientBalanceException(String message) {
super(message);
}
}
// Example of an unchecked exception
public class InvalidUserInputException extends RuntimeException {
public InvalidUserInputException(String message) {
super(message);
}
}
Usage example:
void withdraw(int amount) throws InsufficientBalanceException {
if (balance < amount) {
throw new InsufficientBalanceException("Insufficient balance!");
}
}
Error!try {
// ...
} catch (IOException e) {
throw new CustomAppException("Error processing file", e);
}
try (BufferedReader br = new BufferedReader(new FileReader("test.txt"))) {
// Read file
} catch (IOException e) {
// Handle exception
}
finally block
Comments
Post a Comment