Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 861349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:57:56+00:00 2026-05-15T08:57:56+00:00

When doing null checks in Java code, and you throw IllegalArgumentExceptions for null values,

  • 0

When doing null checks in Java code, and you throw IllegalArgumentExceptions for null values, what kind of message template do you use?

We tend to use something like this

public User getUser(String username){
   if (username == null){
     throw new IllegalArgumentException("username is null");   
   }
   // ...
}

What is better : “is null” or “was null”, and why?

For me “is null” feels more natural.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T08:57:57+00:00Added an answer on May 15, 2026 at 8:57 am

    Since the Exception is thrown due to a failed precondition check, I think rather than simply stating a fact, you should state the requirement that was violated.

    That is, instead of saying "username is null", say "username should not be null".


    On using libraries for precondition checks

    As a tip, you can use one of the many libraries designed to facilitate precondition checks. Many code in Guava uses com.google.common.base.Preconditions

    Simple static methods to be called at the start of your own methods to verify correct arguments and state. This allows constructs such as

     if (count <= 0) {
       throw new IllegalArgumentException("must be positive: " + count);
     }
    

    to be replaced with the more compact

     checkArgument(count > 0, "must be positive: %s", count);
    

    More directly relevant here is that it has checkNotNull, which allows you to simply write:

      checkNotNull(username, "username should not be null");
    

    Note how naturally the above code reads, with the detailed message explicitly stating the requirement that was violated.

    The alternative of stating facts is more awkward:

     // Awkward!
     checkArgument(count > 0, "is negative or zero: %s", count);
     checkNotNull(username, "username is null");
    

    Moreover, this is also potentially less useful, since the client may already be aware of the fact, and the exception doesn’t help them figure out what the actual requirements are.


    On IllegalArgumentException vs NullPointerException

    While your original code throws IllegalArgumentException on null arguments, Guava’s Preconditions.checkNotNull throws NullPointerException instead.

    This is in accordance with the guideline set by the API:

    NullPointerException: Applications should throw instances of this class to indicate other illegal uses of the null object.

    Additionally, here’s a quote from Effective Java 2nd Edition: Item 60: Favor the use of standard exceptions:

    Arguably, all erroneous method invocations boil down to an illegal argument or illegal state, but other exceptions are standardly used for certain kinds of illegal arguments and states. If a caller passes null in some parameter for which null values are prohibited, convention dictates that NullPointerException be thrown rather than IllegalArgumentException.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Java I am told that when doing a null check one should use
how to delete/remove call log from application. I am doing like this this.getContentResolver().delete(CallLog.Calls.CONTENT_URI,null,null); it
The code fails at session.Save(employee); with AssertionFailure null identifier. What am I doing wrong?
I am doing a project in java swing in which I have to use
Method I am unit testing checks for a session variable like if(Session[somevar] != null)
why is doing the following so bad? String val = null; String someOtherValue =
I am doing this: // Member variable List<String> items = null; // in onCreate
Doing an ajax get request works as expected using the following code: $.ajax({ type:
In my java program I make heavy use of Suns implmentation of the Rhino
I'd really like to improve the current readability of my code by reducing the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.