Java Custom Exceptions: Tailoring Error Handling for Your Application
Explore the concept of custom exceptions in Java, which are user-defined exceptions allowing you to manage errors according to your application's specific requirements. Derived from the Exception
class, custom exceptions provide greater flexibility and control in error handling. Learn why you might need custom exceptions and how to implement them effectively to enhance your Java application's robustness and maintainability.
Java - Custom Exception
Java Custom Exception Overview
A custom exception is a user-defined exception that allows you to handle errors according to your application's specific needs. These exceptions are derived from the Exception
class.
Need for Java Custom Exceptions
- To categorize exceptions based on different types of errors in your project.
- To enable application-level exception handling.
Learn Java in-depth with real-world projects through our Java certification course. Enroll to become a certified expert and boost your career.
Creating a Custom Exception in Java
To create a custom exception, define a class that extends the Exception
class.
Syntax
Syntax
class MyException extends Exception {
}
By extending the Exception
class, you create your own checked exception.
Rules for Creating Custom Exceptions
- All exceptions must be a child of
Throwable
. - For a checked exception, extend the
Exception
class. - For a runtime exception, extend the
RuntimeException
class.
Java Custom Exception Example
The InsufficientFundsException
class is a user-defined checked exception:
InsufficientFundsException Class
class InsufficientFundsException extends Exception {
private double amount;
public InsufficientFundsException(double amount) {
this.amount = amount;
}
public double getAmount() {
return amount;
}
}
To use this exception, the CheckingAccount
class contains a withdraw()
method that may throw an InsufficientFundsException
:
CheckingAccount Class
class CheckingAccount {
private double balance;
private int number;
public CheckingAccount(int number) {
this.number = number;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) throws InsufficientFundsException {
if(amount <= balance) {
balance -= amount;
} else {
double needs = amount - balance;
throw new InsufficientFundsException(needs);
}
}
public double getBalance() {
return balance;
}
public int getNumber() {
return number;
}
}
The following BankDemo
program demonstrates the usage of the deposit()
and withdraw()
methods:
BankDemo Program
package com.tutorialsarena;
public class BankDemo {
public static void main(String [] args) {
CheckingAccount c = new CheckingAccount(101);
System.out.println("Depositing $400...");
c.deposit(400.00);
try {
System.out.println("\nWithdrawing $100...");
c.withdraw(100.00);
System.out.println("\nWithdrawing $350...");
c.withdraw(350.00);
} catch (InsufficientFundsException e) {
System.out.println("Sorry, but you are short $" + e.getAmount());
e.printStackTrace();
}
}
}
Output
Output
Depositing $400...
Withdrawing $100...
Withdrawing $350...
Sorry, but you are short $250.0
com.tutorialsarena.InsufficientFundsException
at com.tutorialsarena.CheckingAccount.withdraw(BankDemo.java:39)
at com.tutorialsarena.BankDemo.main(BankDemo.java:14)
Creating an Unchecked Exception
In the next example, we will create an unchecked exception by extending the RuntimeException
class:
Unchecked Exception Example
class InsufficientFundsException extends RuntimeException {
private double amount;
public InsufficientFundsException(double amount) {
this.amount = amount;
}
public double getAmount() {
return amount;
}
}
The following BankDemo
program demonstrates the usage of the deposit and withdraw methods with unchecked exceptions:
BankDemo Program with Unchecked Exception
package com.tutorialsarena;
public class BankDemo {
public static void main(String [] args) {
CheckingAccount c = new CheckingAccount(101);
System.out.println("Depositing $300...");
c.deposit(300.00);
System.out.println("\nWithdrawing $100...");
c.withdraw(100.00);
System.out.println("\nWithdrawing $250...");
c.withdraw(250.00);
}
}
Output
Output
Depositing $300...
Withdrawing $100...
Withdrawing $250...
Exception in thread "main"
com.tutorialsarena.InsufficientFundsException
at com.tutorialsarena.CheckingAccount.withdraw(BankDemo.java:35)
at com.tutorialsarena.BankDemo.main(BankDemo.java:13)