0
0
Javaprogramming~30 mins

Creating custom exception class in Java - Try It Yourself

Choose your learning style9 modes available
Creating custom exception class
πŸ“– Scenario: Imagine you are building a simple banking application. You want to handle errors when someone tries to withdraw more money than their account balance.
🎯 Goal: You will create a custom exception class called InsufficientFundsException and use it to show a clear error message when a withdrawal is not possible.
πŸ“‹ What You'll Learn
Create a custom exception class named InsufficientFundsException that extends Exception.
Add a constructor to InsufficientFundsException that accepts a String message and passes it to the superclass.
Create a class called BankAccount with a private double variable balance.
Add a constructor to BankAccount that sets the initial balance.
Add a method withdraw in BankAccount that throws InsufficientFundsException if withdrawal amount is greater than balance.
Add a method getBalance to return the current balance.
Write code to test withdrawing money and catching the custom exception.
πŸ’‘ Why This Matters
🌍 Real World
Custom exceptions help make error messages clearer and more specific in real applications like banking, shopping carts, or user input validation.
πŸ’Ό Career
Understanding how to create and use custom exceptions is important for writing robust Java programs and is a common skill required in software development jobs.
Progress0 / 4 steps
1
Create the custom exception class
Create a public class called InsufficientFundsException that extends Exception. Add a constructor that takes a String message and passes it to super(message).
Java
Need a hint?

Remember to extend Exception and call super(message) inside the constructor.

2
Create the BankAccount class with balance
Create a public class called BankAccount. Add a private double variable named balance. Add a constructor that takes a double initialBalance and sets balance to it.
Java
Need a hint?

Use private double balance; and set it in the constructor.

3
Add withdraw method with exception
In the BankAccount class, add a public method withdraw that takes a double amount and throws InsufficientFundsException. If amount is greater than balance, throw new InsufficientFundsException with message "Not enough balance". Otherwise, subtract amount from balance. Also add a public method getBalance that returns the current balance.
Java
Need a hint?

Check if amount is more than balance. If yes, throw the exception. Otherwise, reduce the balance.

4
Test withdrawing and catching exception
Create a public class Main with a main method. Inside main, create a BankAccount object with initial balance 1000. Try to withdraw 1500 inside a try block. Catch InsufficientFundsException and print its message. Also print the remaining balance after the withdrawal attempt.
Java
Need a hint?

Use a try block to call withdraw and catch InsufficientFundsException. Print the exception message and then print the balance.