Here's a crude example to at least put forth some effort in asking this question. I tried searching and got some results but I fear my inquiry is too simplistic to find viable results.
Let's say we have just two parameters defined in the code; and in the script tool we have configured parameter 1 to be 'required' and parameter 2 to be 'optional'.
RequiredParameter = arcpy.GetParameterAsText(0)
OptionalParameter = arcpy.GetParameterAsText(1)
So if your code has a line expecting OptionalParameter, the script will fail. So how best to evaluate it being supplied by the user or not?
A few options I have found:
Evaluate for presence of the optional parameter:
if OptionalParameter and OptionalParameter != "#":
#Do something with optional parameter
Try-Except block:
try:
#arcpy tool using the supplied optional parameter
except:
pass #move on with life never knowing if anything went wrong or why it did
I suppose something could be done within the script tool validation, but the code itself would have to be modified as well.
Pitfalls of either? Better options exist?