LearnFrenzy
Sign in Take a free test

Encapsulation · Encapsulation MCQ · Question 4

How should a constructor handle invalid arguments? Answer, explanation & discussion

  • MCQ
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 Back to Exercise

A constructor receives arguments that would make the object invalid. What is the best response?

  • A Store them anyway and fix them later
  • B Throw an exception so that no invalid object is ever created Correct
  • C Print a warning and continue
  • D Set all members to zero silently
Correct Answer: Option B

If the constructor throws, the object never exists, so every existing object satisfies the invariant.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links