14

I want to do null checks for my method arguments, like parameters should not be null. Is it okay to use something like this assertNotNull("Map should not be null", filePaths); in my Java code? I'm trying to avoid

if(filePaths == null){
  throw new IllegalArgumentException("Maps cannot be null");
}

just to keep my code clean from all those null checks. I know I can write a Validator class of my own and have overloaded notNull methods but is there something existing and simple to use to not re-invent the wheel.

The only drawback I see of using JUnit Assert is that it throws AssertionError and not IllegalArgumentException and so forth.

5
  • 3
    Why not use Guava's Preconditions? Jul 12, 2013 at 17:26
  • Another option is to use the assert keyword provided by the Java language -- e.g., assert null != filepaths : "Maps cannot be null". These checks can be turned on or off on startup. Jul 12, 2013 at 17:28
  • 2
    @AndyThomas: Do not use assertions for argument checking in public methods. from Documentation. If assertions are disabled, it will never be called Jul 12, 2013 at 17:32
  • @LouisWasserman: Thanks for your advice, we are not currently using Guava, but it looks like worth giving shot Jul 12, 2013 at 17:33
  • @Learner - Good point. In some special cases I've used assert() for non-public methods. But usually it would be better to stick to a single approach, like Louis' suggestion of Guava's Preconditions. Jul 12, 2013 at 18:45

2 Answers 2

13

If you use Java 7+, you can use:

Objects.requireNonNull(filePaths, "Map should not be null");

Also with a null argument, I would expect a NullPointerException or an IllegalArgumentException, but not an AssertionError.

2
  • Thanks for your response. We re still on Java 6 but good to know that its in built in Java 7 Jul 12, 2013 at 17:34
  • @Learner With Java 6 you can use Guava Preconditions as already pointed out (it does roughly the same thing as Objects.requireNonNull).
    – assylias
    Jul 12, 2013 at 17:45
11

No, it is not OK to use it. The JUnit assert methods throw an AssertionError. It's not a good idea to throw Error in production code. From the javadoc:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it. A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur. That is, Error and its subclasses are regarded as unchecked exceptions for the purposes of compile-time checking of exceptions.

2
  • Even if I write my own validation, I'll still be throwing IllegalArgumentException in production code. Oh you meant throw Exception and not Error Jul 12, 2013 at 17:35
  • Correct, I meant don't throw an Error as opposed to an Exception. Jul 12, 2013 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.