473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

except AttributeError, TypeError:

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
Jul 18 '05 #1
2 5381

"Thorsten Kampe" <th******@thorstenkampe.de> wrote in message
news:19***************@thorstenkampe.de...
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
Thorsten

Jul 18 '05 #2
Thorsten Kampe <th******@thorstenkampe.de> writes:
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.

--
Brian Gough

Network Theory Ltd,
Publishing the Python Manuals --- http://www.network-theory.co.uk/
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: KefX | last post by:
This may have been discussed before, but I'm kind of confused as to why Python doesn't support having both an except ~and~ a finally clause, like this: try: raise RuntimeException except:...
0
by: Erlend Fuglum | last post by:
I have tried and tried, but cannot figure out the source of the following error: AttributeError: 'module' object has no attribute 'menyHMTL' __doc__ = 'Attribute not found.' __getitem__ =...
18
by: marduk | last post by:
I commonly use code like this try: # call optional method myobj.method() except AttributeError: # no biggie pass
32
by: Rene Pijlman | last post by:
One of the things I dislike about Java is the need to declare exceptions as part of an interface or class definition. But perhaps Java got this right... I've writen an application that uses...
4
by: John Salerno | last post by:
My code is below. The main focus would be on the OnStart method. I want to make sure that a positive integer is entered in the input box. At first I tried an if/else clause, then switched to...
5
by: Nebur | last post by:
I'm using the contract.py library, running Python 2.4.4. Now I'm confronted with the following exception backtrace: (...) File "/usr/lib/python2.4/site-packages/contract.py", line 1265, in...
3
by: Patrick C | last post by:
Hey, If i use a try/except clause to get around something like a TypeError, will that immediately bust me out of a while loop? If so, is there any way i can tell it to stay in the while loop...
6
by: Alan Isaac | last post by:
Is the behavior below expected? If so, why is the exception not caught? Thanks, Alan Isaac .... except TypeError: print 'oops' .... Traceback (most recent call last): File "<stdin>", line 1,...
9
by: hajdukzivivjecno | last post by:
I am new user of Python. I would like to change my .shp file that I have astext and it looks something like this (using some loop): to something like this (for every polygon): ((5576309...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.