what is the reasonable (best?) Exception handling strategy?
Duncan Booth
duncan.booth at invalid.invalid
Thu Jun 1 06:44:29 EDT 2006
Petr Jakes wrote:
> I am a little bit confused by all possibilities for exceptions handling
> in Python (probably because I am not skilled enough??) I did try to
> search trough this list and reading Python tutorial about Errors and
> Exceptions but didn't find some "general" answer about exception
> handling policy (strategy).
It depends on what you are actually able to do about the exception. If you
can recover from it meaningfully then you may want to handle it near the
place it is thrown. If all you can do is abort the entire program then you
handle that at the outermost level of the program.
>
> In the following example each row can IMHO raise an exception (if the
> Firebird service is not running for example, if the database is
> corrupted etc.).
If a service isn't running that sounds pretty fatal. Handle it at the outer
levels of your code. If the database is corrupted that might also be
terminal unless you include bad data (e.g. invalid email address) in that
definition, in that case it may be something you can fix, ignore, or live
with: it should be obvious in this case where in your code you need to do
the fixup or ignoring.
>
> Do I have to write "try/except" clause on each row?
The processing you perform on a row might raise an exception for which the
correct action would be to simply continue with the next row. In that case
handle the exception inside the 'processRow' function so the code which
iterates over the rows never sees it. If it is a more serious problem which
is going to stop you processing any further rows then you let it propogate.
>
> Or to write try/except block (function) where to handle (on one place)
> all exceptions expected in the program code is a good idea?
>
> Or do I have to write own "exception hook"?
>
> What about unexpected exceptions? :(
Big errors, or unexpected errors you handle in one place usually by making
sure a human is alerted to the problem.
More information about the Python-list
mailing list