Java Exception Handling Introduction
In this page:
try {
// code that may throw
} catch (ExceptionType e) {
// handle
}
What is an Exception?
An exception is an object Java creates and throws when something disrupts a program's normal flow, like dividing by zero or accessing a null reference.
Instead of letting the program crash outright, Java lets you catch that object and decide how to respond.
Example: What is an Exception?
public class Main {
public static void main(String[] args) {
try {
int result = 10 / 0; // throws ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Caught: " + e.getMessage());
}
}
}
Login to try C/C++/Java/PHP code in the editor
Checked vs Unchecked Exceptions
Checked exceptions (like IOException) must be declared or caught at compile time because the compiler forces you to acknowledge the failure is possible; unchecked exceptions (like NullPointerException) extend RuntimeException and can surface without any such warning.
Example: Checked vs Unchecked Exceptions
public class Main {
public static void main(String[] args) {
try {
throw new RuntimeException("Unchecked"); // no need to declare it
} catch (RuntimeException e) {
System.out.println(e.getMessage());
}
}
}
Login to try C/C++/Java/PHP code in the editor
Why Handle Exceptions?
Handling exceptions keeps one failure from silently corrupting program state or crashing the whole application. It also lets you give the user a meaningful message instead of a raw stack trace, or fall back to a safe default and keep running.
Example: Why Handle Exceptions?
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Try code that might throw an exception
try {
// Declare arr and set it to new int[2]
int[] arr = new int[2];
// Print a line to the console
System.out.println(arr[5]);
// Handle ArrayIndexOutOfBoundsException if it was thrown
} catch (ArrayIndexOutOfBoundsException e) {
// Print a line to the console
System.out.println("Handled gracefully instead of crashing");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Common Exception Types
Some of the exceptions you'll hit constantly include NullPointerException (calling a method on null), ArrayIndexOutOfBoundsException (invalid array index), and ArithmeticException (like dividing an int by zero).
Recognizing these by name speeds up debugging considerably.
Example: Common Exception Types
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Try code that might throw an exception
try {
// Declare s and set it to null
String s = null;
s.length();
// Handle NullPointerException if it was thrown
} catch (NullPointerException e) {
// Print a line to the console
System.out.println("Caught a NullPointerException");
}
}
}
Login to try C/C++/Java/PHP code in the editor
The try-catch-finally Concept
The try block holds code that might fail, catch defines how to respond if it does, and finally runs regardless of whether an exception occurred — typically used to release resources like open files or network connections.
Example: The try-catch-finally Concept
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Try code that might throw an exception
try {
// Declare result and set it to 10 / 0
int result = 10 / 0;
// Handle ArithmeticException if it was thrown
} catch (ArithmeticException e) {
// Print a line to the console
System.out.println("Error occurred");
// Always runs, whether or not an exception occurred
} finally {
// Print a line to the console
System.out.println("Runs regardless");
}
}
}
Login to try C/C++/Java/PHP code in the editor
- Catching
Exceptiontoo broadly and hiding real bugs. - Ignoring checked exceptions, which the compiler forces you to handle or declare.
- Leaving an empty
catchblock, so failures are silent.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: