When to Throw IllegalArgumentException or NullPointerException for a Null Parameter
When defining setter methods, developers often face the dilemma of whether to throw an IllegalArgumentException or a NullPointerException for a null parameter. Both exceptions appear relevant based on their JavaDoc descriptions:
However, the appropriate choice is IllegalArgumentException (IAE) for the following reasons:
Therefore, for setter methods where null is not appropriate, IllegalArgumentException should be used to convey the intent of parameter validation.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3