0
0
Javaprogramming~30 mins

Throwing custom exceptions in Java - Mini Project: Build & Apply

Choose your learning style9 modes available
Throwing custom exceptions
πŸ“– Scenario: Imagine you are building a simple banking application. You want to make sure that users cannot withdraw more money than they have in their account. To do this, you will create a custom exception called InsufficientFundsException and throw it when a withdrawal amount is too large.
🎯 Goal: Build a Java program that defines a custom exception InsufficientFundsException, uses it in a BankAccount class, and throws this exception when a withdrawal is attempted with insufficient balance.
πŸ“‹ What You'll Learn
Create a custom exception class called InsufficientFundsException that extends Exception.
Create a BankAccount class with a balance variable.
Add a method withdraw that throws InsufficientFundsException if withdrawal amount is greater than balance.
Write code to catch the InsufficientFundsException and print a friendly message.
πŸ’‘ Why This Matters
🌍 Real World
Custom exceptions are used in real-world applications to handle specific error cases clearly, such as banking errors, file handling problems, or invalid user input.
πŸ’Ό Career
Knowing how to create and throw custom exceptions is important for writing robust Java applications and is a common requirement 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 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 balance
Create a public class called BankAccount. Inside it, declare 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 as private and initialize it in the constructor.

3
Add withdraw method that throws the custom exception
Inside the BankAccount class, add a public method called withdraw that takes a double parameter amount and throws InsufficientFundsException. If amount is greater than balance, throw a new InsufficientFundsException with the message "Insufficient funds for withdrawal". Otherwise, subtract amount from balance.
Java
Need a hint?

Use throw new InsufficientFundsException("Insufficient funds for withdrawal") when the amount is too large.

4
Test withdrawal and catch the exception
Create a public class called Main with a main method. Inside main, create a BankAccount object with initial balance 100.0. Use a try-catch block to call withdraw(150.0) on the account. Catch InsufficientFundsException and print the exception message using System.out.println(e.getMessage()).
Java
Need a hint?

Use a try-catch block to catch InsufficientFundsException and print the message.