I have a question regarding best practices.
Lets say I have this class:
public class test {
int value = 0;
int value2 = 0;
boolean valid = true;
test(int a, int b) {
if (a>5){
this.value = a;
}
else{
this.valid = false;
return;
}
if (b>100){
this.value2=b;
}
else{
this.valid = false;
return;
}
}
public static void main(String[] args){
test t = new test(6,120);
System.out.println(t.valid);
System.out.println(t.value);
System.out.println(t.value2);
}
}
As you can see, I want to construct the class, but I also want to check if the values are between an expected range. If not, forget anything else, because the result will be wrong. In that case, it would be useless to continue constructing the object, so I can also exit right away.
Using a return does the job, as the outputs prove at the end. But is that considered best practice? Or should I rather raise exceptions? Or does it not matter?
Using an exception I could see which one exactly failed, but I could also just print it out...
Thanks!
thx.