JS
JV

JavaScript to Java

10 lessons

Progress0%
1Variables & Types2Functions & Methods3Arrays & Collections4Classes & OOP5Exception Handling6Async vs Threads7Generics8String Methods9Interfaces and Abstract Classes10Build Tools and Ecosystem
All Mirror Courses
JS
JV
Exception Handling
MirrorLesson 5 of 10
Lesson 5

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.

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with dealing with errors and failures.

JV
In Java:

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:

JV
Java 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:

JS
JavaScript (What you know)
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");
}
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

Java has checked exceptions (must declare or catch) and unchecked (extends RuntimeException)

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

Java catch specifies the exception type; JS catch catches everything

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

Java multi-catch: catch (A | B e) combines multiple types

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

JV
In Java:

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.

JS
JavaScript
catch (err) { if (err instanceof ValidationError) { ... } }
JV
Java
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.

JV
Java
// 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.

JV
Java
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
Common Pitfall
Don't assume Java works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in Java to practice these concepts.
PreviousNext