I want to make some code that just converts an int of value 0 or 1 to a boolean but I don’t want the value inputted to be anything other than 0 or 1. If that is the case I want to throw an error and stop the program like any other error would normally do.
How should I do this? I tried searching around for how to use exceptions and throwing errors but I only got results that were about using “try” and “catch” which would not work in this case.
Here is my code:
public boolean intToBoolean(int value){
// Intended for only 1 and 0
return value == 1 ? true : false;
}
void setup(){
size(400,400);
background(0);
println( intToBoolean(1) );
println( intToBoolean(0) );
println( intToBoolean(4) );
println( intToBoolean(0) );
}
public boolean intToBoolean(int value){
// Intended for only 1 and 0 - all other values throw an error.
if (value != 0 && value != 1) {
throw( new RuntimeException("Value was not either 0 or 1.") );
}
return( value == 1 );
}