0
0
Javaprogramming~30 mins

Why custom exceptions are needed in Java - See It in Action

Choose your learning style9 modes available
Why Custom Exceptions Are Needed
πŸ“– Scenario: Imagine you are building a simple banking application. You want to handle errors like when someone tries to withdraw more money than they have. Java has built-in exceptions, but sometimes you need your own special error messages to make your program clearer and easier to fix.
🎯 Goal: You will create a custom exception called InsufficientFundsException to show how custom exceptions help make your program clearer and handle specific problems better.
πŸ“‹ What You'll Learn
Create a custom exception class called InsufficientFundsException that extends Exception
Create a class called BankAccount with a balance variable
Add a method withdraw in BankAccount that throws InsufficientFundsException if withdrawal amount is more than balance
Write code to catch the InsufficientFundsException and print a clear message
πŸ’‘ Why This Matters
🌍 Real World
Custom exceptions are used in real banking software to handle errors like insufficient funds clearly and safely.
πŸ’Ό Career
Understanding custom exceptions is important for writing clean, maintainable Java code and is a common skill required in software development jobs.
Progress0 / 4 steps
1
Create the InsufficientFundsException class
Create a public class called InsufficientFundsException that extends Exception. Add a constructor that takes a String message and passes it to the superclass constructor using super(message).
Java
Need a hint?

Remember, to create a custom exception, extend the Exception class and call super(message) in the constructor.

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

Define the balance variable and set it in the constructor using this.balance = initialBalance.

3
Add the withdraw method that throws InsufficientFundsException
In the BankAccount class, add a public method called withdraw that takes a double parameter amount and throws InsufficientFundsException. Inside the method, check if amount is greater than balance. If yes, throw a new InsufficientFundsException with the message "Not enough balance". Otherwise, subtract amount from balance.
Java
Need a hint?

Use throws InsufficientFundsException in the method signature and throw the exception when amount is too big.

4
Test the withdraw method and catch the exception
Create a public class called Main with a main method. Inside main, create a BankAccount object with a balance of 100. Use a try-catch block to call withdraw(150) on the account. Catch InsufficientFundsException and print the exception message.
Java
Need a hint?

Use a try-catch block to call withdraw and catch InsufficientFundsException. Print the exception message with e.getMessage().