# Usually pretty much any code is error-prone, and not because you're bad
# programmer (be sure - you are, I am as well bad programmer), but because
# environment is wild and dangerous. Lets calculate sum of all numbers from
# `integers.txt` file, single integer per line.
# Too much constrints to let this code be alive so easily...
try:
with open('integers.txt') as f:
numbers = map(int, f.readlines())
total = sum(numbers)
if total > 10:
raise OverflowError("More than I can handle")
print total
# What if there's no such file? Stop code?
except IOError as e:
print "IOError: {0}".format(e)
# What if there's wrong value to be an integer? Or you forget to split lines?
except ValueError as e:
print "ValueError: {0}".format(e)
# Ok, now it's your own fault...
except OverflowError as e:
print "OverflowError: {0}".format(e)
# But if nothing happens, then you can surely execute `esle` statement
else:
print "Whoooo! That was close..."
1
2
3