Correct use of try,except and raise?

Ben Finney bignose+hates-spam at benfinney.id.au
Sun Jul 13 07:44:40 EDT 2008


Bart Kastermans <kasterma at bart-kastermanss-macbook.local> writes:

> Roy Smith <roy at panix.com> writes:
> > The defensive thing to do is catch exactly the exception you
> > expect to happen. In this case, that means IndexError.
> 
> And you do that by
> 
> except IndexError:
>    raise TheErrorYouNowWantToRaise

You only do that if you want to throw away important information, such
as the traceback associated with the original exception. Not very
friendly to debugging.

> And
> except IndexError, e:
> 
> if you want access to the exception as well.

Usually best if it can be achieved. Not least because the bare 'raise'
statement will re-raise the original exception, complete with all its
context.

-- 
 \         “If we don't believe in freedom of expression for people we |
  `\           despise, we don't believe in it at all.” —Noam Chomsky, |
_o__)                                                       1992-11-25 |
Ben Finney



More information about the Python-list mailing list