[Solved] Custom Exception – Bank Withdrawal with Java

Custom Exception – Bank Withdrawal: A customer goes to the ATM to withdraw money from his account. The maximum withdrawal limit in ICICI bank for a day is Rs. 25,000.

Write a program that accepts the withdrawal amount from the customer as input. When the amount entered by the user is greater than the withdrawal limit (Rs. 25000), a custom exception named MinimumAccountBalance is thrown. Use exception handling mechanisms to handle this exception.

Input Format:

The input consists of a double value which corresponds to the amount to withdraw.

Output Format:
If the amount to withdraw is less than Rs.25,000 print “Amount Withdrawn Successfully”. Else throw the custom exception.
Refer to sample input and output for formatting specifications.


Note: All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output 1:

Enter amount to withdrawal
5000
Amount Withdrawn Successfully

Sample Input and Output 2:

Enter amount to withdrawal
26000
Caught MinimumAccountBalance: Minimum Account Balance Exception

Solution

import java.util.Scanner;
class MinimumAccountBalance extends Exception {
    public MinimumAccountBalance(String message) {
        super(message);
    }
}
        //sauravhathi
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //sauravhathi
        System.out.println("Enter amount to withdrawal");
        double amount = sc.nextDouble();
        try {
            if(amount>25000) {
                throw new MinimumAccountBalance("Minimum Account Balance Exception");
            }
            else {
                        //sauravhathi
                System.out.println("Amount Withdrawn Successfully");
            }
        }
        catch(MinimumAccountBalance e) {
            System.out.println("Caught MinimumAccountBalance: "+e.getMessage());
        }
    }
            //sauravhathi
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

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