[Tutor] error trap

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 18 Jan 2002 13:37:13 -0800 (PST)


On Fri, 18 Jan 2002, kirk Bailey wrote:

> intresting. If I read this right, if I say
> 
> Exception enception e:
> 
> it will accept ANY exception??? and e will capture the string fed to
> the thing and be printable later?

Yes, if we do a try/except block with:

###
try:
    ...
except Exception, e:
    ....
###

barring catastrophic failure, this should allow you to catch everything.


Now that you know about this, it's important to say that is usually not
such a good idea to have a catch-all unless the situation requires it.


Having something like:

###
try:
   ...
except IndexError, e:
   ...
###

is useful, because it's saying "we do expect that it's possible IndexError
will occur, and in that case, we can handle this particular situation with
grace."


But:

###
try:
    ...
except Exception, e:
    ...
###

is often a sign of: "I don't want my system to just die on me, even if it
should.  I just want it to keep chugging along, despite all
circumstances."