Java Bootcamp – Workbooks and Challenges
)}

Movie Store – Part 6

The goal of Part 6 is to bulletproof the application. A bulletproof application:

  • Is free of bugs (behaves like we want it to).

  • Doesn't fail/crash no matter what.

Exception Handling

Exception Handling involves:

  1. Catching checked exceptions (outside the application's control).

  2. Fixing unchecked exceptions (resulting from badly written code).

You already caught the checked exceptions. Now, you will fix unchecked exceptions.

Task 1 – IllegalArgumentException


Thrown to indicate that a method has been passed an illegal or inappropriate argument.

If the user tries to enter a blank movie, the Movie constructor will throw an IllegalArgumentException.

Screen Shot 2021-06-18 at 11.03.08 PM.png

In such a scenario, println("\n\nThe input you provided is not valid. Please try again\n"); and restart the while loop.

Task 2 – IllegalStateException


Thrown to indicate that a method has been called at a "bad" time.

If the user tries to purchase a movie that was already rented, the action method will throw an IllegalStateException.

Screen Shot 2021-06-18 at 11.06.29 PM.png

If the requested movie is not available.

  • System.out.println("\n\n\n\nThe movie is not available for purchase. Please try again\n");

  • restart the while loop.

Hint: Create another getMovie method inside the Store class. The second getMovie method accepts String name and returns a Movie object.

By the way, creating another method of the same name with different parameters is called overloading.

Your code is now bulletproof!

No matter what the user throws at you, the application is free of bugs and will never crash. It can process any input and respond with grace.

Untitled.gif

Feedback Summary
4.8
39 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.