(License: CC BY-SA 4.0)
If statement:
value = count_inputs(...)
if value < -1:
print("Error in counting inputs!")
exit -1
# now do stuff with value
...
Exception:
try:
value = count_inputs(...)
# now do stuff with value
...
except:
print("Error in counting inputs!")
raise
BaseException
except
clauses to catch specific exceptions:import sys
try:
f = open('myfile.txt')
s = f.readline()
i = int(s.strip())
except OSError as err:
print("OS error: {0}".format(err))
except ValueError:
print("Could not convert data to an integer.")
except:
print("Unexpected error:", sys.exc_info()[0])
raise