Read amount of I/Os in code
It depends on the core, but most provide the macro NUM_DIGITAL_PINS which tells you the number of digital pins the board has.  In general this doesn't equate to the actual number, but to one more than the highest number (there are boards with gaps in the pin number sequence).

So you can use:

if (pinNumber &lt; NUM_DIGITAL_PINS) {
    digitalWrite(pinNumber, HIGH);
} else {
    Serial.println("Invalid pin number");
}


In tandem with that you also usually have NUM_ANALOG_INPUTS.