0Pricing
Java Academy · Lesson

Throwing & Custom Exceptions

Learn to throw exceptions explicitly and define custom exception classes.

Throwing & Custom Exceptions is a free Java Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Intro

Sometimes you need to signal an error yourself. Java lets you throw exceptions explicitly and even create custom types.

Throw keyword

The throw statement creates and sends an exception. Use it inside methods to indicate something went wrong.

Code: throw built-in

Here we throw an IllegalArgumentException when age is too small.

public class Main {
  static void checkAge(int age) {
    if (age < 18) {
      throw new IllegalArgumentException("Must be 18 or older");
    }
    System.out.println("Access granted");
  }
  public static void main(String[] args) {
    try {
      checkAge(15);
    } catch (IllegalArgumentException e) {
      System.out.println("Caught: " + e.getMessage());
    }
  }
}

Custom exceptions

You can make your own custom exceptions. For checked exceptions, extend Exception. For unchecked, extend RuntimeException.

Code: custom exception

This shows a custom checked exception. Methods must declare it with throws.

public class Main {
  // Custom checked exception
  static class InvalidAgeException extends Exception {
    InvalidAgeException(String msg) { super(msg); }
  }
  static void validate(int age) throws InvalidAgeException {
    if (age < 18) throw new InvalidAgeException("Too young!");
  }
  public static void main(String[] args) {
    try {
      validate(15);
    } catch (InvalidAgeException e) {
      System.out.println("Caught custom: " + e.getMessage());
    }
  }
}

Code: runtime custom

Custom runtime exceptions extend RuntimeException. They do not need to be declared.

public class Main {
  // Custom unchecked exception
  static class FatalError extends RuntimeException {
    FatalError(String msg) { super(msg); }
  }
  public static void main(String[] args) {
    try {
      throw new FatalError("Something bad happened");
    } catch (FatalError e) {
      System.out.println("Caught runtime: " + e.getMessage());
    }
  }
}

Custom exception check

Quick check: How do you define a custom checked exception?

Recap

Recap: Use throw to signal errors. Extend Exception for checked or RuntimeException for unchecked custom exceptions.

Frequently asked questions

Is the “Throwing & Custom Exceptions” lesson free?

Yes — the full text of “Throwing & Custom Exceptions” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Throwing & Custom Exceptions”?

Learn to throw exceptions explicitly and define custom exception classes. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Throwing & Custom Exceptions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Throwing & Custom Exceptions
  2. try-with-resources (AutoCloseable)
  3. Defensive Programming
← Back to Java Academy