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 6876371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:28:08+00:00 2026-05-27T04:28:08+00:00

This is a topic in one of our code reviews, I’d like more opinions.

  • 0

This is a topic in one of our code reviews, I’d like more opinions.

Let’s say I am writing a service that will allow me to insert a simple Person object into a database.

public class Person
{
   private String name;
   ....
}

We have a simple VerifyNotNull method throws IllegalArgumentException.

Which route would you take and why.

Option 1:

Verify not null in constructor of Person object.

public Person(String name)
{
     VerifyNotNull(name);//throws illegal arg exception if name is null
     this.name = name;
}

Option 2:

Allow Person to be constructed with null, verify not null on addPerson call.

public class PersonService
{
  public void addPerson(Person personToAdd)
  {
     VerifyNotNull(personToAdd.getName());//throws illegal arg exception
     //add code
  }
}

I don’t like the idea of throwing Exceptions in constructors. To me option 2 feels right, I don’t know how to explain or justify it though.

Is it acceptable to throws Exceptions in constructors?

Thanks for your help!

  • 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-27T04:28:09+00:00Added an answer on May 27, 2026 at 4:28 am

    The first approach is more fail-fast, which will increase the likelihood that you’ll find the source of your bug more quickly. Think of it like this: if your error log starts telling you that a number of errors have been cropping up because you’re trying to add people that have null names, you’re going to want to know where those people’s null names came from, right? Depending on how your system is structured, it’s possible that the person was created miles away from the place where the person is getting added to the service. So you have no idea which of the four thousand places in your code is creating people without names.

    So if I had to choose, I’d go with the first option.

    Of course, it depends on your business model. If a person will a null name is a perfectly legal thing to create when you’re in the data-entry phase, and it’s not until you’re getting ready to persist that person’s information that you want to make sure it’s passed validation, then that’s a different story. In that case, you might even want to come up with a ValidatedPerson class that wraps Person, but indicates in a type-safe way that the addPerson method can only be called if someone has gone through the validation process, because the only way to create a ValidationPerson is through a specific validate method that checks the person’s name.

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

Sidebar

Related Questions

This question is related to this topic. I changed the syntax to one that
sorry, i know this lots on this topic, but i couldnt find one where
Our connectivity to EMS code was initially ill-designed and created one TopicConnection object per
Is my second question on this topic today but on the previous one it
I know, that this topic is already well documented here . But I couldn't
I've found plenty of other topics similar to this one, but I they are
This topic has been covered on this forums, but I haven't found a working
This topic has been scratched once or twice, but I am still puzzled. And
This topic can look similar to others but I haven't found any usable answer
I'm new to this topic so sorry if this is a stupid question :\

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.