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:
-
Catching checked exceptions (outside the application's control).
-
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
.
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
.
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.