11

To be more specific, I want to write a code that throws IllegalArgumentException if the given value is negative. Should I include this code inside of setter/constructor or should I check the value when the appropriate method is called? (Eg: start(), init(), print() or run(). whatever.)

My code (simplified):

public class LLUAlgorithm {

private int temperature;

public int getTemperature() {
    return temperature;
}

public void setTemperature(int temperature) {
    if (temperature < 0)
        throw new IllegalArgumentException("can't be smaller than 0.")
    this.temperature = temperature;
}

public void run() {
    ...
}   

I don't recall a single case that a setter throws an exception as above. But I'm curious if it is good / bad.

leventunver
  • 3,269
  • 7
  • 24
  • 39
  • 4
    Well, the exception name contains _illegal argument_, so of course you can throw it, when an argument was illegal, like a negative number. And yes, this can happen in a setter and I never heard, that this would be bad practice (because it isn't). – Tom May 07 '16 at 19:04
  • 1
    yes, it is ok, you could even try to be more specific with the exception... if is null, if is invalid index or define the custom exception – ΦXocę 웃 Пepeúpa ツ May 07 '16 at 19:04
  • 3
    You should throw IllegalArgumentException. You can overdo it with a builder pattern. Did you read the book "Effective Java", that has material about the builder pattern. – Niklas Rosencrantz May 07 '16 at 19:05
  • Yes, throw the exception in the setter. If your code throws `IllegalArgumentException` at some later point in time, it would be confusing to the caller. From [the docs](https://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.html): `Thrown to indicate that a method has been passed an illegal or inappropriate argument.` – izrik May 07 '16 at 19:05
  • I think its good to put exception in setters in to give the model itself to ability to talk about the error no matter how many times the model has been used. also reduce amount of exception handling each time you use that. Moreover if you can think of exposing APIs, thats also sounds good enough to put exception in setter – Syed Ekram Uddin May 07 '16 at 19:11
  • Yes this is good style. – Louis Wasserman May 07 '16 at 19:34

1 Answers1

6

The best approach is to make your object immutable, get rid of your setters and throws your exception in the constructor otherwise whatever the way you choose, in case of an error, you have a high risk to have your object in an inconsistent state which will cause hard bug to find. For a better understanding, please read this especially the section related to Failure Atomicity.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
  • So I get the idea. If I remove the setter in my case and put the exception inside the constructor, then the object won't even be created. Otherwise, the object will be in a broken state. Thanks a lot, for both the answer and the link. – leventunver May 07 '16 at 19:28