When to use try and except?

Craig Allen callen314 at gmail.com
Sat Aug 30 04:05:13 CEST 2008


On Aug 29, 7:23 am, cnb <circularf... at yahoo.se> wrote:
> If I get zero division error it is obv a poor solution to do try and
> except since it can be solved with an if-clause.
>
> However if a program runs out of memory I should just let it crash
> right? Because if not then I'd have to write exceptions everywhere to
> prevent that right?
well, recovering from an error can simply be telling the user what
happened before bailing, so given that a program out of memory is
going to fail, that doesn't mean you shouldn't catch the error and
fail more gracefully.

Of course, memory is a particularly hard example because you may not
have memory to be preparing output...

> So when would I actually use try-except?
>
> If there can be several exceptions and I just want to catch 1 or 2?
> Like
> try:
>     blahaba
> except SomeError:
>     do something




More information about the Python-list mailing list