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.