The new
exception class developed by a developer is called custom exception or
user-defined exception. These classes must be sub class of either Throwable or
any of its sub class. Most of the cases in projects custom exception classes
are derived from Exception class. All exception classes defined by SUN are
based on the java language and API requirements. Hence according to the
business requirements developer must write their own exception class.
Example –
public class
Exception1 {
public static void
main(String[] args) {
int i=5;
try{
if(i<10){
throw new MyException("Number2");
}
}
catch(Exception
e)
{
System.out.println(e);
}
}
}
class MyException extends
Exception{
public
MyException(String msg)
{
super(msg);
}
}
Output –
excep.MyException: Number2
Note – In the above example, we
have created our own exception class MyException
which extends super class Exception. And calling its super class constructor
we are printing the message whatever we want, we always have to invoke super
class constructor to execute user-defined exception. Through try block we are
passing the exception using throw keyword to user-defined exception class.
Program for custom exceptions
InvalidAmountException, InsufficientFundsException to handle wrong operations
done by customer in deposit and withdraw operations.
Test
cases are –
- · Throw InvalidAmountException if user enter zero or –ve number in deposit or withdraw.
- · Throw InsufficientFundsException if user amount > balance
import java.io.IOException;
import
java.util.Scanner;
class InvalidAmountException
extends Exception{
public
InvalidAmountException(){
super();
}
public
InvalidAmountException(String msg)
{
super(msg);
}
}
class InsufficientFundsException
extends Exception{
public
InsufficientFundsException(){
super();
}
public
InsufficientFundsException(String msg){
super(msg);
}
}
interface
Bank{
public void
deposit(double amt) throws InvalidAmountException;
public double
withdraw(double amt)throws InsufficientFundsException;
public void
balanceEnquiry();
}
class
ICICI implements Bank{
private double balance;
@Override
public void
balanceEnquiry() {
System.out.println("Current balance : "+balance);
}
@Override
public void
deposit(double amt) throws InvalidAmountException {
if(amt<=0)
{
throw new
InvalidAmountException(amt+" is
invalid amount");
}
balance
= balance+amt;
}
@Override
public double
withdraw(double amt) throws InsufficientFundsException {
if(balance<amt)
{
throw new
InsufficientFundsException("Insufficient
funds");
}
balance=balance-amt;
return amt;
}
}
public class
Exception1 {
public static void
main(String[] args) {
Bank acc
= new ICICI();
try{
Scanner
sc = new Scanner(System.in);
String
option;
do{
System.out.println("1 deposit");
System.out.println("2 withdraw");
System.out.println("3 balance enquiry");
System.out.println("Enter option : ");
option=sc.next();
switch(option)
{
case "1":
{
System.out.println("Enter deposit amount : ");
double amt
= sc.nextDouble();
acc.deposit(amt);
acc.balanceEnquiry();
break;
}
case "2":
{
System.out.println("Enter withdraw amount : ");
double amt
= sc.nextDouble();
acc.withdraw(amt);
acc.balanceEnquiry();
break;
}
case "3":
{
acc.balanceEnquiry();
break;
}
default:
System.out.println("Invalid option");
}
System.out.println("Do you want to continue yes/no");
option=sc.next();
}while(option.equalsIgnoreCase("Yes"));
}
catch(InvalidAmountException
ie){
System.out.println(ie.getMessage());
}
catch(InsufficientFundsException
is){
System.out.println(is.getMessage());
}
catch(NumberFormatException
nfe){
System.out.println("please enter number only");
}
}
}
Output –
1 deposit
2 withdraw
3 balance enquiry
Enter option :
3
Current balance : 0.0
Do you want to continue yes/no
yes
1 deposit
2 withdraw
3 balance enquiry
Enter option :
1
Enter deposit amount :
15000.00
Current balance : 15000.0
Do you want to continue yes/no
yes
1 deposit
2 withdraw
3 balance enquiry
Enter option :
2
Enter withdraw amount :
10000
Current balance : 5000.0
Do you want to continue yes/no
yes
1 deposit
2 withdraw
3 balance enquiry
Enter option :
2
Enter withdraw amount :
10000
Insufficient funds
No comments:
Post a Comment