Sometimes I see programmers that use “assert”, and other times I see null checking for parameters and throws Exception.
What’s the best option?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Take always into account that an “assert” is not always enabled. This is the main rule you should have always in your mind. Think of it as a mechanism to detect bugs in your code, unexpected situations that should not happen (pre/postconditions). On the other hand, throw an exception (i.e. IllegalArgumentException) if there is a chance to get that null value from the caller.
For example: