Building Robust Booking System with Exception Handling

Business Scenario

Pre-Lab Preparation

  • Exception and its causes

  • Important blocks in exception handling

  • Types of exception

  • Creating custom exception classes

Task 1: Declaring the Passenger class

1

In order to store the passenger data, we need to declare the Passenger Class

Task 2: Accepting Passenger Input from user

1

Create the class PassengerData, that defines the main method. Through this method user input will be accepted.

2

run the code and check the output

Task 3: Exception handling, while accepting the input ‘age’

1

Write the try-catch block to accept user input and handling the invalid input

2

run the code to check the output

Task 4: Throwing an exception to validate the passport number

1

modify the setter method, to accept the passport no with length 7

2

Change the main method code, to handle the exception if entered passport no is invalid

3

run the code to check the output

Task 5: Creating a custom exception, for invalid passport no

1

define the exception class InvalidPassportNumber

2

Modify the setter of the Passenger Class

3

3

Change the main method, to handle this exception

4

run the code to check the output

 

Great job!

  • We have understood how to apply exception handling concepts for validating user inputs

  • We have understood creating custom exceptions, throwing them manually and handling them in application

Checkpoint

Java_Lab_14

By Content ITV

Java_Lab_14

  • 6