As stated by and Macattack and Jab, you could use try except, which you can read about in python's docs or w3school's tutorial.
Try Except clauses have the form:
try:
# write your code
pass
except Exception as e: # which can be ValueError, or other's exceptions
# deal with Exception, and it can be called using the variable e
print(f"Exception was {e}") # python >= 3.7
pass
except Exception as e: # for dealing with other Exception
pass
# ... as many exceptions you would need to handle
finally:
# do something after dealing with the Exception
pass
For a list of built-in Exceptions, see python's docs.