Java Bootcamp – Workbooks and Challenges
)}

Contacts – Part 5

Goal: Quality control the ContactManager class.

Unchecked exceptions


An unchecked exception crashes the app as a result of badly written code.

You should throw an:

  • IllegalArgumentException when the caller passes faulty arguments into a method/constructor.

  • IllegalStateException when the caller invokes a method at a bad time (object not in a valid state).

Throwing an unchecked exception forces the caller to improve their code.

Task 1 – Inspecting the ContactManager class


1.  The constructor doesn't receive any parameters so there's nothing to check.

2.  Should the setContact setter check for a null?

    -   Nope. The code would already throw a NullPointerException. So, throwing an IllegalArgumentException would be redundant.

3.  Should the addContact method check for a null?

    -   Nope. The code would already throw a NullPointerException. So, throwing an IllegalArgumentException would be redundant.

4.  Should removeContact check for a null?

    -   Nope. The code would already throw a NullPointerException. So, throwing an IllegalArgumentException would be redundant.

5.  Is there a need to throw an IllegalStateException anywhere?

    -   Yes. If the contacts  ArrayList is empty, then the manager is not in a valid state to call the removeContact method.

That's all!

You added a checkpoint in the ContactManager class. It forbids the caller from misusing removeContact.

Feedback Summary
4.8
39 students
5

92%
4

3%
3

0%
2

0%
1

5%
Written Reviews
There are no written reviews yet.