Skip to the content.

Java - Exceptions - Hands-on

Learning Objectives


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:

  1. Accept two integer inputs.
  2. Perform division and handle any exceptions.
  3. Display an appropriate error message if an exception occurs.

Expected Output:


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:

  1. Create an array with predefined values.
  2. Accept an index as input.
  3. Handle the exception if the index is out of bounds.

Expected Output:


Exercise 3: Handling Multiple Exceptions

Problem Statement

A program reads an integer from user input. Handle both InputMismatchException and ArithmeticException.

Instructions:

  1. Accept an integer input.
  2. Perform division with a fixed number.
  3. Handle exceptions appropriately.

Expected Output:


Exercise 4: Using Finally Block

Problem Statement

Demonstrate the use of the finally block in exception handling.

Instructions:

  1. Implement a try-catch block with a finally statement.
  2. Show that the finally block executes regardless of exceptions.

Expected Output:


Exercise 5: Creating a Custom Exception

Problem Statement

Create a custom exception InvalidAgeException to handle age validation in a voting system.

Instructions:

  1. Define a custom exception class InvalidAgeException.
  2. Implement a method that throws this exception for ages below 18.
  3. Handle the exception and display a proper message.

Expected Output:


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:

  1. Accept multiple age inputs.
  2. Validate each age and throw the custom exception if itโ€™s invalid.
  3. Display appropriate messages for valid and invalid ages.

Expected Output:


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:

  1. Accept a filename as input.
  2. Try to open the file and read its content.
  3. Use nested try-catch blocks to handle different exceptions separately.

Expected Output:


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:

  1. Create a BankAccount class with name and balance fields.
  2. Implement deposit and withdraw methods.
  3. Implement InsufficientFundsException to be thrown if withdrawal exceeds balance.
  4. Handle the exception and display an appropriate message.

Expected Output:


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:

  1. Create a Flight class with available seat numbers stored in a list.
  2. Implement a bookSeat method that checks if the seat is available.
  3. If the seat is unavailable, throw InvalidSeatException.
  4. Handle the exception and display an appropriate message.

Expected Output:


๐Ÿ”— Related Topics: