Sure, here’s an example of how to create and use a custom exception in Java:

Custom exceptions should be used when you want to handle specific error scenarios in your application that are not covered by the built-in exception classes in Java.

When you throw a custom exception, you can provide a more meaningful and specific error message that describes the reason for the exception. This makes it easier for developers to understand and troubleshoot issues that may arise during the execution of the application.

In addition, custom exceptions can be used to provide more detailed information about the cause of the exception, such as the stack trace or additional metadata. This information can be useful for debugging and diagnosing issues in the application.

Finally, custom exceptions can also be used to encapsulate business logic or rules within the application. By creating custom exceptions that represent specific failure conditions or errors in the business logic, you can enforce those rules and make your code more expressive and readable.

Overall, using custom exceptions can help you write more robust, maintainable, and understandable code in Java.

Here’s an example of how to create and use a custom exception in Java:

public class InsufficientFundsException extends Exception {
    public InsufficientFundsException(String message) {

public class BankAccount {
    private double balance;

    public BankAccount(double initialBalance) {
        balance = initialBalance;

    public void withdraw(double amount) throws InsufficientFundsException {
        if (amount > balance) {
            throw new InsufficientFundsException("Insufficient funds.");
        } else {
            balance -= amount;
            System.out.println("Withdrawal of $" + amount + " successful. Current balance: $" + balance);

public class Main {
    public static void main(String[] args) {
        BankAccount account = new BankAccount(1000);

        try {
        } catch (InsufficientFundsException e) {

This program defines a custom exception called InsufficientFundsException, which is thrown when a bank account has insufficient funds to complete a withdrawal.

The BankAccount class has a method called withdraw that takes a double parameter representing the amount to withdraw. If the amount is greater than the current balance, the method throws an InsufficientFundsException with a message indicating that the withdrawal cannot be completed. Otherwise, the method subtracts the withdrawal amount from the balance and prints a message indicating the withdrawal was successful.

The Main class creates a new BankAccount with an initial balance of $1000. It then attempts to withdraw $500 and $800 from the account using the withdraw method. Since the second withdrawal exceeds the account balance, an InsufficientFundsException is thrown and caught in a try-catch block. The program outputs the message “Insufficient funds.” to indicate that the withdrawal failed.


So empty here ... leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.