To be more specific, I want to write code that throws an IllegalArgumentException if the given value is negative. Should I include this code inside setter / constructor or should I check the value when calling the appropriate method? (For example: 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 do not remember a single case when the setter throws an exception, as indicated above. But I'm curious if this is good / bad.
source share