Java OCA OCP Practice Question 643

Question

Which of the following is the best scenario to use an exception?

  • A. The computer caught fire.
  • B. The code does not compile.
  • C. A caller passes invalid data to a method.
  • D. A method finishes sooner than expected.


C.

Note

Option A is incorrect.

You should probably seek help if the computer is on fire!

Option B is incorrect because code that does not compile cannot run and therefore cannot throw any exceptions.

Option C is the best answer, since an IllegalArgumentException can be used to alert a caller of missing or invalid data.

Option D is incorrect; finishing sooner is rarely considered a problem.




PreviousNext

Related