How do I create a custom exception in Java?

You can create a custom exception to describe a problem specific to your application. Give it a meaningful name so calling code can recognize and handle the problem.

In this example, you will create an InvalidAgeException and throw it when someone under 18 tries to register.

Source code

Save the following code as CustomExceptionExample.java:

public class CustomExceptionExample {
    void main() {
        try {
            register(16);
        } catch (InvalidAgeException exception) {
            IO.println("Registration failed: " + exception.getMessage());
        }

        IO.println("The program continues.");
    }

    void register(int age) throws InvalidAgeException {
        if (age < 18) {
            throw new InvalidAgeException(
                    "You must be at least 18 years old.");
        }

        IO.println("Registration successful.");
    }
}

class InvalidAgeException extends Exception {
    InvalidAgeException(String message) {
        super(message);
    }
}

Explanation

The custom exception extends Exception:

class InvalidAgeException extends Exception {
    InvalidAgeException(String message) {
        super(message);
    }
}

This makes InvalidAgeException a checked exception. Code that calls a method declaring this exception must either catch it or declare that it can propagate it.

The constructor accepts a message and passes it to the superclass constructor:

super(message);

Calling code can retrieve that message using getMessage().

Declaring the exception

The register() method declares the exception using throws:

void register(int age) throws InvalidAgeException {

This declaration tells callers that the method may throw an InvalidAgeException.

Throwing the exception

The method checks whether the age meets the registration requirement:

if (age < 18) {
    throw new InvalidAgeException(
            "You must be at least 18 years old.");
}

The throw statement creates and throws the exception. Execution leaves the method immediately, so the success message is skipped.

Notice the difference between the two keywords:

Keyword Purpose
throw Throws a specific exception object
throws Declares exceptions that a method may propagate

Handling the exception

The calling code catches the custom exception:

try {
    register(16);
} catch (InvalidAgeException exception) {
    IO.println("Registration failed: " + exception.getMessage());
}

Since 16 is below the minimum age, the catch block prints the failure message.

Once the exception is handled, the program continues after the try-catch statement.

Run the example

Compile and run the file:

javac CustomExceptionExample.java
java CustomExceptionExample

Output

Registration failed: You must be at least 18 years old.
The program continues.

If you change the method call to register(20), the output becomes:

Registration successful.
The program continues.

Choosing an exception type

Extend Exception when you want callers to catch or declare the exception. Extend RuntimeException when you want an unchecked exception, which callers are not required to catch or declare.

Use a custom exception when its name gives callers useful information or lets them handle a specific failure separately. For a simple invalid argument, a standard exception such as IllegalArgumentException may be sufficient.

How do I handle an exception using try-catch in Java?

You can use a try-catch statement to handle an exception and provide a useful response when an operation fails.

In this example, you will try to convert text into an integer. If the text is not a valid integer, the program will display a message and continue running.

Source code

Save the following code as TryCatchExample.java:

public class TryCatchExample {
    void main() {
        String input = "twenty";

        try {
            int number = Integer.parseInt(input);
            IO.println("Parsed number: " + number);
        } catch (NumberFormatException exception) {
            IO.println("Cannot convert \"" + input + "\" to an integer.");
            IO.println("Please enter a whole number, such as 20.");
        }

        IO.println("The program continues.");
    }
}

Explanation

The variable input contains the text to convert:

String input = "twenty";

The try block contains the operation that may throw an exception:

try {
    int number = Integer.parseInt(input);
    IO.println("Parsed number: " + number);
}

Integer.parseInt() converts a valid integer string, such as "20", into an int.

If the text cannot be parsed as an int, the method throws a NumberFormatException. This also happens when the number is outside the range supported by int.

Catching the exception

The catch block handles NumberFormatException:

catch (NumberFormatException exception) {
    IO.println("Cannot convert \"" + input + "\" to an integer.");
    IO.println("Please enter a whole number, such as 20.");
}

Since "twenty" is not a valid integer string, execution moves from Integer.parseInt() to this block.

The remaining statement inside the try block is skipped, so "Parsed number: ..." is not printed.

The parameter exception refers to the exception object. This example provides its own message rather than displaying the exception’s technical details.

Continuing after the catch block

After the exception is handled, execution continues with the statement following the try-catch:

IO.println("The program continues.");

Catching an exception does not retry the failed operation automatically.

Run the example

Compile and run the file:

javac TryCatchExample.java
java TryCatchExample

Output

Cannot convert "twenty" to an integer.
Please enter a whole number, such as 20.
The program continues.

If you change input to "20":

String input = "20";

The conversion succeeds, the catch block is skipped, and the output becomes:

Parsed number: 20
The program continues.

Catch the specific exception you can handle. Here, NumberFormatException clearly identifies the expected conversion failure.

How do I create a record to store data in Java?

A record is a concise way to define a class that holds data. Java automatically provides a constructor, accessor methods, and implementations of equals(), hashCode(), and toString().

In this example, you will create a Book record to store a title and an author.

Source code

Save the following code as RecordExample.java:

public class RecordExample {
    void main() {
        Book firstBook = new Book("Learning Java", "Alice");
        Book secondBook = new Book("Learning Java", "Alice");

        IO.println("Title: " + firstBook.title());
        IO.println("Author: " + firstBook.author());
        IO.println("Record: " + firstBook);
        IO.println("Equal values: " + firstBook.equals(secondBook));
        IO.println("Same object: " + (firstBook == secondBook));
    }
}

record Book(String title, String author) {
}

Explanation

The following declaration defines a record with two components:

record Book(String title, String author) {
}

The components are title and author, both of type String.

For this declaration, Java automatically provides:

Member Purpose
Book(String title, String author) Initializes the components
title() Returns the title
author() Returns the author
equals(Object other) Compares component values
hashCode() Calculates a hash code from the components
toString() Returns a text representation of the record

Creating a record object

Use the new keyword and pass a value for each component:

Book firstBook = new Book("Learning Java", "Alice");

The arguments follow the order of the components in the record declaration.

Reading component values

Record accessors use the component names directly:

firstBook.title()
firstBook.author()

The generated methods are named title() and author(), rather than getTitle() and getAuthor().

Each component has a private final field. Records do not generate setters, so you cannot reassign these fields after construction.

Comparing records

The example creates two separate objects with the same component values:

Book firstBook = new Book("Learning Java", "Alice");
Book secondBook = new Book("Learning Java", "Alice");

The generated equals() method returns true because both objects are instances of the same record class and have equal component values.

The == operator returns false because the variables refer to separate objects.

Printing a record

Passing the record to IO.println() uses its generated toString() method:

IO.println("Record: " + firstBook);

The result includes the record name, component names, and their values.

Run the example

Compile and run the file:

javac RecordExample.java
java RecordExample

Output

Title: Learning Java
Author: Alice
Record: Book[title=Learning Java, author=Alice]
Equal values: true
Same object: false

Are records immutable?

Records are shallowly immutable: their component fields cannot be reassigned, but referenced objects may still be mutable.

This Book record contains only strings, which are immutable. If a record contains a mutable list or array, additional steps are needed to protect that data from changes.

How do I define and use an enum in Java?

An enum defines a fixed set of named constants. You can use it to represent values such as order statuses, directions, or days of the week.

In this example, you will define an enum for order statuses and use a switch expression to select a message.

Source code

Save the following code as EnumExample.java:

public class EnumExample {
    void main() {
        OrderStatus status = OrderStatus.SHIPPED;

        String message = switch (status) {
            case PENDING -> "Your order is waiting for confirmation.";
            case PROCESSING -> "Your order is being prepared.";
            case SHIPPED -> "Your order is on its way.";
            case DELIVERED -> "Your order has arrived.";
        };

        IO.println("Current status: " + status);
        IO.println("Message: " + message);

        IO.println();
        IO.println("Available statuses:");

        for (OrderStatus availableStatus : OrderStatus.values()) {
            IO.println(availableStatus);
        }
    }
}

enum OrderStatus {
    PENDING,
    PROCESSING,
    SHIPPED,
    DELIVERED
}

Explanation

The enum keyword declares a type with a fixed set of constants:

enum OrderStatus {
    PENDING,
    PROCESSING,
    SHIPPED,
    DELIVERED
}

Enum constant names conventionally use uppercase letters, with underscores separating words.

Assigning an enum value

Use the enum type followed by a constant name:

OrderStatus status = OrderStatus.SHIPPED;

The variable status can hold a reference to an OrderStatus constant. You cannot assign a string such as "SHIPPED" directly to it.

This helps prevent mistakes caused by spelling or capitalization differences in string values.

Using an enum in a switch expression

The switch expression selects a message for the current status:

String message = switch (status) {
    case PENDING -> "Your order is waiting for confirmation.";
    case PROCESSING -> "Your order is being prepared.";
    case SHIPPED -> "Your order is on its way.";
    case DELIVERED -> "Your order has arrived.";
};

Inside these case labels, you can use the constant names without the OrderStatus. prefix.

The expression covers every constant declared in OrderStatus, so a default rule is unnecessary.

Looping through enum constants

Every enum provides a values() method that returns an array of its constants in declaration order:

for (OrderStatus availableStatus : OrderStatus.values()) {
    IO.println(availableStatus);
}

Here, the loop prints all four statuses, starting with PENDING.

Run the example

Compile and run the file:

javac EnumExample.java
java EnumExample

Output

Current status: SHIPPED
Message: Your order is on its way.

Available statuses:
PENDING
PROCESSING
SHIPPED
DELIVERED

Use an enum when a value should come from a known set of choices. It gives those choices meaningful names and lets the compiler check their usage.

How do I override a method in Java?

Method overriding lets a subclass provide its own implementation of an inherited instance method.

In this example, you will override the printDetails() method so an ebook displays its file format along with its title and author.

Source code

Save the following code as MethodOverridingExample.java:

public class MethodOverridingExample {
    void main() {
        Book book = new Book("Learning Java", "Alice");
        Book ebook = new Ebook("Exploring Java", "Bob", "PDF");

        book.printDetails();
        IO.println();
        ebook.printDetails();
    }
}

class Book {
    private final String title;
    private final String author;

    Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    public void printDetails() {
        IO.println("Title: " + title);
        IO.println("Author: " + author);
    }
}

class Ebook extends Book {
    private final String fileFormat;

    Ebook(String title, String author, String fileFormat) {
        super(title, author);
        this.fileFormat = fileFormat;
    }

    @Override
    public void printDetails() {
        super.printDetails();
        IO.println("File format: " + fileFormat);
    }
}

Explanation

The Book class defines a method that prints its title and author:

public void printDetails() {
    IO.println("Title: " + title);
    IO.println("Author: " + author);
}

The Ebook class extends Book and overrides that method:

@Override
public void printDetails() {
    super.printDetails();
    IO.println("File format: " + fileFormat);
}

The overriding method has the same name and parameter list as the inherited method. Here, it also has the same return type, void.

Using the @Override annotation

The @Override annotation tells the compiler that the method is intended to override an inherited method.

If you misspell the method name or use the wrong parameters, the compiler reports an error. This helps catch mistakes before running the program.

Calling the superclass implementation

Inside the overriding method, this statement calls the implementation in Book:

super.printDetails();

It prints the title and author. The next statement adds the ebook’s file format:

IO.println("File format: " + fileFormat);

Calling super.printDetails() is optional. This example uses it to reuse the existing printing code.

Selecting the method at runtime

The following variable has the declared type Book, but refers to an Ebook object:

Book ebook = new Ebook("Exploring Java", "Bob", "PDF");

When you call:

ebook.printDetails();

Java runs the implementation in Ebook because that is the object’s actual class. This behavior is called runtime polymorphism.

Run the example

Compile and run the file:

javac MethodOverridingExample.java
java MethodOverridingExample

Output

Title: Learning Java
Author: Alice

Title: Exploring Java
Author: Bob
File format: PDF

Overriding changes an inherited instance method’s behavior in a subclass. Overloading, covered earlier, defines methods with the same name but different parameter lists.