Cathing several potential errors?

C

cnb

if i do
try:
something
except TypeError, IndexError:
pass

only the first error will get caught. I dont want to use Exception and
catch all errors, but just 2. how can i do that?
 
G

Gary Josack

cnb said:
if i do
try:
something
except TypeError, IndexError:
pass

only the first error will get caught. I dont want to use Exception and
catch all errors, but just 2. how can i do that?
what you're doing is assigning the value of TypeError to the name IndexError

try:
somthing
except (TypeError, IndexError):
pass

use

except (TypeError, IndexError), e:
print e

if you want to print the error
 
J

John Machin

if i do
try:
    something
except TypeError, IndexError:
    pass

only the first error will get caught. I dont want to use Exception and
catch all errors, but just 2. how can i do that?

The syntax for what you can have between "except" and ":" is:
[expression ["," target]]
'target' is an optional name to be bound to the actual exception
object that is raised.
If you want to catch multiple kinds of exception, 'expression' needs
to be a tuple.

See
http://docs.python.org/tut/node10.html#SECTION0010300000000000000000
and
http://docs.python.org/ref/try.html

Cheers,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,293
Latest member
Hue Tran

Latest Threads

Top