The try
statement lets us capture an exception. When an exception is raised, we have a number of choices for handling it:
- Ignore it: If we do nothing, the program stops. We can do this in two ways—don't use a
try
statement in the first place, or don't have a matchingexcept
clause in thetry
statement. - Log it: We can write a message and let it propagate; generally this will stop the program.
- Recover from it: We can write an
except
clause to do some recovery action to undo the effects of something that was only partially completed in thetry
clause. We can take this a step further and wrap thetry
statement in awhile
statement and keep retrying until it succeeds. - Silence it: If we do nothing (that is,
pass
) then processing is resumed after thetry
statement. This silences the exception. - Rewrite it: We can raise a different exception. The original exception becomes a context for the newly-raised exception.
- Chain it: We chain a different exception to the original...