Java - Exceptions - Hands-on
Learning Objectives
- Understand how to handle exceptions in Java.
- Implement try-catch-finally blocks effectively.
- Create and use custom exceptions in Java.
Exercise 1: Handling Arithmetic Exceptions
Problem Statement
A program calculates the division of two numbers. Handle the ArithmeticException when a division by zero occurs.
Instructions:
- Accept two integer inputs.
- Perform division and handle any exceptions.
- Display an appropriate error message if an exception occurs.
Expected Output:
- The result of the division.
- An error message if division by zero is attempted.
Exercise 2: Handling Array Index Out of Bounds
Problem Statement
Write a program that accesses an array element using an index given by the user. Handle the ArrayIndexOutOfBoundsException.
Instructions:
- Create an array with predefined values.
- Accept an index as input.
- Handle the exception if the index is out of bounds.
Expected Output:
- The value at the given index.
- An error message if the index is invalid.
Exercise 3: Handling Multiple Exceptions
Problem Statement
A program reads an integer from user input. Handle both InputMismatchException and ArithmeticException.
Instructions:
- Accept an integer input.
- Perform division with a fixed number.
- Handle exceptions appropriately.
Expected Output:
- The division result.
- An error message if invalid input or division by zero occurs.
Exercise 4: Using Finally Block
Problem Statement
Demonstrate the use of the finally block in exception handling.
Instructions:
- Implement a try-catch block with a
finallystatement. - Show that the
finallyblock executes regardless of exceptions.
Expected Output:
- The result of the try block.
- Confirmation that the
finallyblock is executed.
Exercise 5: Creating a Custom Exception
Problem Statement
Create a custom exception InvalidAgeException to handle age validation in a voting system.
Instructions:
- Define a custom exception class
InvalidAgeException. - Implement a method that throws this exception for ages below 18.
- Handle the exception and display a proper message.
Expected Output:
- Confirmation if the age is valid.
- An error message if the age is invalid.
Exercise 6: Throwing and Handling Custom Exceptions
Problem Statement
Modify the previous exercise to include user input and validate multiple ages in a loop.
Instructions:
- Accept multiple age inputs.
- Validate each age and throw the custom exception if itโs invalid.
- Display appropriate messages for valid and invalid ages.
Expected Output:
- Confirmation messages for valid ages.
- Exception messages for invalid ages.
Exercise 7: Nested Try-Catch for File Handling
Problem Statement
A program reads a file and processes the content. Implement nested try-catch blocks to handle FileNotFoundException and IOException separately.
Instructions:
- Accept a filename as input.
- Try to open the file and read its content.
- Use nested try-catch blocks to handle different exceptions separately.
Expected Output:
- File content if successful.
- Appropriate error messages for missing or unreadable files.
Exercise 8: Banking System with Insufficient Funds Exception
Problem Statement
Simulate a banking system where users can withdraw money. Implement a custom exception InsufficientFundsException when withdrawal exceeds the account balance.
Instructions:
- Create a
BankAccountclass withnameandbalancefields. - Implement
depositandwithdrawmethods. - Implement
InsufficientFundsExceptionto be thrown if withdrawal exceeds balance. - Handle the exception and display an appropriate message.
Expected Output:
- Updated balance after withdrawal.
- Error message if withdrawal exceeds available balance.
Exercise 9: Flight Booking System with Invalid Seat Exception
Problem Statement
Develop a flight booking system that assigns seats to passengers. Implement a custom exception InvalidSeatException when a passenger selects an unavailable or non-existent seat.
Instructions:
- Create a
Flightclass with available seat numbers stored in a list. - Implement a
bookSeatmethod that checks if the seat is available. - If the seat is unavailable, throw
InvalidSeatException. - Handle the exception and display an appropriate message.
Expected Output:
- Confirmation message for successful seat booking.
- Error message if the seat is invalid or already booked.
๐ Related Topics: