User-Defined Custom Exception in Java

Last Updated : 4 Dec, 2026

A user-defined custom exception is an exception class created by a programmer to represent application-specific errors, such as insufficient account balance or invalid registration age.

  • Custom exceptions can be checked or unchecked.
  • They allow callers to identify and handle specific error conditions separately.

Types of Custom Exceptions

Custom exceptions are classified into two types based on their parent class.

custom_expectation
  • Checked Exceptions: Extend Exception or one of its checked subclasses and must be caught or declared using throws.
  • Unchecked Exceptions: Extend RuntimeException or its subclasses and do not require mandatory handling or declaration.

Creating a Custom Exception

  • Create a class extending Exception or RuntimeException.
  • Optionally, define a constructor that accepts an error message and passes it to the parent class using super(message).
  • Use the throw keyword to raise the exception when an error occurs.

Syntax:

class CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}

Checked Custom Exception

A checked custom exception is suitable when the caller is expected to handle or propagate an application-specific error.

Example: Validating the minimum age for registration.

Java
class InvalidAgeException extends Exception{
    public InvalidAgeException(String msg){
        super(msg);
    }
}

class Geeks{
    static void validate(int age) throws InvalidAgeException {
        if (age < 18) {
            throw new InvalidAgeException("Age must be 18 or above.");
        }

        System.out.println("Registration successful");
    }

    public static void main(String[] args) {
        try {
            validate(15);
        } catch (InvalidAgeException e) {
            System.out.println(e.getMessage());
        }
    }
}

Output
Age must be 18 or above.

Explanation:

  • InvalidAgeException extends Exception, making it a checked exception.
  • The validate() method declares and throws the exception when the age is below 18.
  • The catch block handles the exception and displays its message.

Unchecked Custom Exception

An unchecked custom exception can represent invalid operations or violations of application rules without requiring explicit exception handling.

Example: Withdrawing an amount greater than the available balance.

Java
class InsufficientBalanceException extends RuntimeException{
    public InsufficientBalanceException(String msg){
        super(msg);
    }
}

class Geeks{
    static void withdraw(int balance, int amount){
        if (amount > balance){
            throw new InsufficientBalanceException("Insufficient balance.");
        }

        System.out.println("Withdrawal successful");
    }

    public static void main(String[] args){
        try {
            withdraw(5000, 7000);
        } catch (InsufficientBalanceException e){
            System.out.println(e.getMessage());
        }
    }
}

Output
Insufficient balance.

Explanation:

  • InsufficientBalanceException extends RuntimeException, making it an unchecked exception.
  • The withdraw() method throws the exception when the requested amount exceeds the available balance.
  • The compiler does not require the exception to be caught or declared.

Checked Vs Unchecked Custom Exceptions

Checked Custom Exception

Unchecked Custom Exception

Extends Exception or one of its checked subclasses.

Extends RuntimeException or one of its subclasses.

Exception-handling requirements are enforced at compile time.

Exception-handling requirements are not enforced at compile time.

Must be caught or declared using throws.

Catching or declaring the exception is optional.

Suitable for errors callers are expected to handle or propagate.

Suitable for invalid operations or application rule violations.

Note: Prefer built-in exceptions when they adequately describe an error. Create custom exceptions when an application requires a distinct exception type.

Comment