Exception Handling
Dealing with errors and failures
Introduction
In this lesson, you'll learn about exception handling in Java. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In JavaScript, you're familiar with dealing with errors and failures.
Java has its own approach to dealing with errors and failures, which we'll explore step by step.
The Java Way
Let's see how Java handles this concept. Here's a typical example:
// Custom exception (unchecked)
public class ValidationException extends RuntimeException {
public ValidationException(String message) {
super(message);
}
}
public static int validateAge(int age) {
if (age < 0) throw new ValidationException("Age cannot be negative");
if (age > 150) throw new ValidationException("Age too large");
return age;
}
try {
validateAge(-1);
} catch (ValidationException e) {
System.out.println("Validation: " + e.getMessage());
} catch (Exception e) {
throw e; // rethrow
} finally {
System.out.println("always runs");
}Comparing to JavaScript
Here's how you might have written similar code in JavaScript:
class ValidationError extends Error {
constructor(message) {
super(message);
this.name = "ValidationError";
}
}
function validateAge(age) {
if (age < 0) throw new ValidationError("Age cannot be negative");
if (age > 150) throw new ValidationError("Age too large");
return age;
}
try {
validateAge(-1);
} catch (err) {
if (err instanceof ValidationError) {
console.log("Validation:", err.message);
} else {
throw err; // rethrow
}
} finally {
console.log("always runs");
}You may be used to different syntax or behavior.
Java has checked exceptions (must declare or catch) and unchecked (extends RuntimeException)
You may be used to different syntax or behavior.
Java catch specifies the exception type; JS catch catches everything
You may be used to different syntax or behavior.
Java multi-catch: catch (A | B e) combines multiple types
You may be used to different syntax or behavior.
Java try-with-resources auto-closes AutoCloseable resources
Step-by-Step Breakdown
1. Typed Catches
Java catch clauses specify the exception type — no need for instanceof checks inside the catch block.
catch (err) { if (err instanceof ValidationError) { ... } }catch (ValidationException e) { // type-specific
System.out.println(e.getMessage());
}2. Checked vs Unchecked
RuntimeException subclasses are unchecked (like JS). Exception subclasses are checked — the compiler forces you to handle them.
// Checked: must declare throws or catch
public void readFile() throws IOException { ... }
// Unchecked: no declaration needed
throw new IllegalArgumentException("bad input");3. try-with-resources
Java automatically closes AutoCloseable resources (files, streams) at the end of a try block — no finally needed.
try (var reader = new FileReader("file.txt")) {
// reader automatically closed
}Common Mistakes
When coming from JavaScript, developers often make these mistakes:
- Java has checked exceptions (must declare or catch) and unchecked (extends RuntimeException)
- Java catch specifies the exception type; JS catch catches everything
- Java multi-catch: catch (A | B e) combines multiple types
Key Takeaways
- Java catch types exceptions directly; JS uses instanceof
- Checked exceptions (IOException) must be declared or caught
- RuntimeException subclasses are unchecked
- try-with-resources auto-closes files and streams