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

  • Home
  • SEARCH
  • 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 7435747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:07:35+00:00 2026-05-29T10:07:35+00:00

Does it good idea to have custom exception like IllegalArgumentException and throw it in

  • 0

Does it good idea to have custom exception like IllegalArgumentException and throw it in all cases when methods can get null reference instead of valid object value?

 public void method(String str){
        if(str == null)throw new CustomIllegalArgumentException("str cannot be null");
    }

I think that such way I can always see difference between such illegal argument exceptions and other RuntimeExceptions.

Does it good idea or not?

P.S.: I looked at such posts like
Avoiding != null statements

**UPDATE:**So I will know that is programmer-intended exception and I will have clear log.

  • 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-29T10:07:36+00:00Added an answer on May 29, 2026 at 10:07 am

    IllegalArgumentException is a standard, not a custom exception. It is conventional to throw NullPointerException when argument is null when it shouldn’t be.

    You should in general prefer standard exceptions when they are suitable to your special case. See also item 60 in “Effective Java 2nd edition” (“Favor the use of standard exceptions”). One advantage of this is that you can write a single handler for similar conditions which may occur both in your own code and in libraries you use.

    In order to differentiate the exceptions, you should use the string message they carry. Also, the stack trace will indicate whether the exception has been thrown from your own code or from other code. No need for extra exception class.

    One case when it may be reasonable to create your own exception class is when you need the exception to carry extra information about the exceptional condition it indicates. In this case you should still derive the exception from the appropriate standard class, so that a single handler can be written that handles exceptions for similar conditions coming from both your own code and libraries you use.

    See also Preconditons utility class from google. In particular checkNotNull() utility method which also throws NullPointerException when its argument is null.

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

Sidebar

Related Questions

Googling this does little good, as you can imagine. Does anyone have resources that
While running my application (which does a good bit of custom drawing) I get
When does it become a good idea to have your class separate into two
Does someone have a good idea/solution how to achieve this? Situation is: I have
Is it a good idea to combine C++ and C# or does it pose
this module does a good job at creating a tagcloud block - all good
Does gwt have (good) support for effects, moving around elements, and resizing them? Are
In Java, all numeric types extend from java.lang.Number. Would it be a good idea
I have implemented a custom CellRenderer in PyGTK that can take longer to render
I have a Social Graph assignment, and I have a pretty good idea of

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.