Lets say we have the following code:
print("...")
might_throw_type_error()
print("...")
might_throw_index_error()
If we want to handle the exceptions that those functions might arise what is the preferred way:
Full split of "business" code and error handling
try:
print("...")
might_throw_type_error()
print("...")
might_throw_index_error()
except IndexError:
# index error handling logic
raise
except TypeError:
# index error handling logic
raise
Split of logic and error handling but try starting at the first statement that might raise
print("...")
try:
might_throw_type_error()
print("...")
might_throw_index_error()
except IndexError:
# index error handling logic
raise
except TypeError:
# index error handling logic
raise
Exception handling should only wrap statements we expect to raise
print("...")
try:
might_throw_type_error()
except TypeError:
# index error handling logic
raise
print("...")
try:
might_throw_index_error()
except IndexError:
# index error handling logic
raise
Note that if we capture the exception we don't want to continue