Java Exceptions — Free Java Tutorial
Learn Java Exceptions in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
TL;DR: Learn Java Exceptions in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
Written & reviewed by the Syllab.in Academic Team (CBSE/NCERT subject experts) · Updated
Java Exceptions in Java
An exception is an unexpected event that disrupts normal program flow: dividing by zero, accessing a null reference, reading a file that doesn't exist. Java has a robust exception handling system using try-catch-finally blocks.
try: code that might throw an exception. catch: what to do when a specific exception occurs. finally: code that ALWAYS runs (use for cleanup — closing files, connections). throw: manually throw an exception. throws: declare that a method might throw an exception.
Checked exceptions (must be handled — IOException, SQLException) vs Unchecked exceptions (extend RuntimeException — NullPointerException, ArrayIndexOutOfBoundsException, NumberFormatException). Understanding the difference is important for Java exams.
Java Exceptions — Syntax
try {
// risky code
} catch (ExceptionType e) {
// handle error
System.out.println(e.getMessage());
} finally {
// always executes
}
Learn Java Exceptions step by step with Syllab's free interactive Java tutorial — runnable code examples, practice exercises and instant AI feedback, all free with no signup. Explore the full Java course →