Java OCA OCP Practice Question 433

Question

Which of the following is the most appropriate way to handle invalid arguments in a public method?

  • A. Throw java.lang.InvalidArgumentException.
  • B. Throw java.lang.IllegalArgumentException.
  • C. Check for argument validity in an assert statement, which throws AssertionError when the arguments are invalid.
  • D. Use non-assert code to check for argument validity. If invalid arguments are detected, explicitly throw AssertionError.


B.

Note

Assertions should not be used to check preconditions in a public method.

Some kind of runtime exception should be thrown.

IllegalArgumentException is the most appropriate class name for this situation.

There is no such thing as java.lang.InvalidArgumentException.




PreviousNext

Related