except AttributeError, TypeError:

T

Thorsten Kampe

What is the best way to except two errors, when the except handling in
both cases is the same?

"except:" would just except every error and not just those I want.

except Attribute error:
do_much_stuff
except TypeError:
do_the_same_stuff

is bad because of the duplication and

def do_it():
do_much_stuff
except Attribute error:
do_it()
except TypeError:
do_it()

would work but is rather unelegant? What is the best way to except to
errors with the same exception handling?

Thorsten
 
J

John Roth

Thorsten Kampe said:
What is the best way to except two errors, when the except handling in
both cases is the same?

"except:" would just except every error and not just those I want.

except Attribute error:
do_much_stuff
except TypeError:
do_the_same_stuff

is bad because of the duplication and

def do_it():
do_much_stuff
except Attribute error:
do_it()
except TypeError:
do_it()

would work but is rather unelegant? What is the best way to except to
errors with the same exception handling?

except (AttributeError, TypeError):

John Roth
 
B

Brian Gough

Thorsten Kampe said:
What is the best way to except two errors, when the except handling in
both cases is the same?

The "except" keyword can take a list of exceptions in parentheses,

except (AttributeError, TypeError):
...

See the "Errors and Exceptions" chapter in the Python tutorial for
more details and examples.
 

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
474,196
Messages
2,571,036
Members
47,631
Latest member
kukuh

Latest Threads

Top