Ans:
Sometimes we need to create custom exception in java, i.e exception which is not defined in JDK or any third party library your application using. We often feel a need to create and thrown our own exceptions these exceptions are known as user defined or custom exceptions.
Example
public class UserDefinedException {
public static void main(String args[]) {
Account acct = new Account();
System.out.println("Current balance : " + acct.balance());
System.out.println("Withdrawing $200");
acct.withdraw(200);
System.out.println("Current balance : " + acct.balance());
acct.withdraw(1000);
}
}
/**
* Java class to represent a Bank account which holds your balance and provide wi
*/
class Account {
private int balance = 1000;
public int balance() {
return balance;
}
public void withdraw(int amount) throws NotSufficientFundException {
if (amount > balance) {
throw new NotSufficientFundException(String.format("Current balance %d is less than requested amount %d", balance, amount));
}
balance = balance - amount;
}
public void deposit(int amount) {
if (amount <= 0) {
throw new IllegalArgumentException(String.format("Invalid deposit amount %s", amount));
}
}
}
/**
* User defined Exception class in Java
*/
class NotSufficientFundException extends RuntimeException {
private String message;
public NotSufficientFundException(String message) {
this.message = message;
}
public NotSufficientFundException(Throwable cause, String message) {
super(cause);
this.message = message;
}
public String getMessage() {
return message;
}
}
Out put:
Current balance : 1000 Withdrawing $200 Current balance : 800 Exception in thread "main" excptiohandleing.NotSufficientFundException: Current balance 800 is less than requested amount 1000 at excptiohandleing.Account.withdraw(UserDefinedException.java:30) at excptiohandleing.UserDefinedException.main(UserDefinedException.java:11)