473,378 Members | 1,412 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,378 software developers and data experts.

blanket except clause -- best practice?

[python 2.3.2, SuSE Linux 8.2, x86]
I have a bunch of blanket "except:" clauses like:
[OK, it's not "my" code but I use it and it troubles me...]

class DatabaseError(StandardError): pass
class OperationalError(StandardError): pass

try:
stuff
except _pg.error, msg:
raise DatabaseError, "error '%s' in '%s'" % ( msg, sql )
except:
raise OperationalError, "internal error in '%s'" % sql
This accomplishes passing useful info, namely "sql", on with
the exception. Unfortunately it *loses* info in that upstream
has no way to know *which* exception triggered this or what args
it might have been given. I'm trying to think of a clean way
to improve this. I could do:

try:
stuff
except _pg.error, msg:
raise DatabaseError, "error '%s' in '%s'" % ( msg, sql )
except Exception, x:
raise OperationalError, "internal %s(%s) error in '%s'" (x.__class__,
x.args, sql)
Is there something more clear/portable/pythonic than this?
How do people handle this sort of fallback "except" clause?

-- George
Jul 18 '05 #1
3 2470
The following code do nothing:
import sys
try:
stuff
except:
type,val,tb = sys.exc_info()
raise type,val,tb

With this should be able to do what you want.

Boris

On Tue, 28 Oct 2003 12:21:54 -0500, George Young wrote:
[python 2.3.2, SuSE Linux 8.2, x86]
I have a bunch of blanket "except:" clauses like:
[OK, it's not "my" code but I use it and it troubles me...]

class DatabaseError(StandardError): pass
class OperationalError(StandardError): pass

try:
stuff
except _pg.error, msg:
raise DatabaseError, "error '%s' in '%s'" % ( msg, sql )
except:
raise OperationalError, "internal error in '%s'" % sql
This accomplishes passing useful info, namely "sql", on with
the exception. Unfortunately it *loses* info in that upstream
has no way to know *which* exception triggered this or what args
it might have been given. I'm trying to think of a clean way
to improve this. I could do:

try:
stuff
except _pg.error, msg:
raise DatabaseError, "error '%s' in '%s'" % ( msg, sql )
except Exception, x:
raise OperationalError, "internal %s(%s) error in '%s'" (x.__class__,
x.args, sql)
Is there something more clear/portable/pythonic than this?
How do people handle this sort of fallback "except" clause?

-- George


Jul 18 '05 #2

George> class DatabaseError(StandardError): pass
George> class OperationalError(StandardError): pass

George> try:
George> stuff
George> except _pg.error, msg:
George> raise DatabaseError, "error '%s' in '%s'" % ( msg, sql )
George> except:
George> raise OperationalError, "internal error in '%s'" % sql

George> This accomplishes passing useful info, namely "sql", on with the
George> exception. Unfortunately it *loses* info in that upstream has
George> no way to know *which* exception triggered this or what args it
George> might have been given. I'm trying to think of a clean way to
George> improve this. I could do:

George> try:
George> stuff
George> except _pg.error, msg:
George> raise DatabaseError, "error '%s' in '%s'" % ( msg, sql )
George> except Exception, x:
George> raise OperationalError, "internal %s(%s) error in '%s'" (x.__class__, x.args, sql)

Perhaps you should simply reraise the original exception:

try:
stuff
except _pg.error, msg:
raise DatabaseError, "error '%s' in '%s'" % ( msg, sql )
except:
raise

The resulting traceback displayed may be lower level than you would like,
but at least no information about where the actual error occurred is lost.

You can also synthesize a new exception using the original stack frame:

try:
stuff
except _pg.error, msg:
raise DatabaseError, "error '%s' in '%s'" % ( msg, sql )
except Exception, x:
raise OperationalError, \
"internal %s(%s) error in '%s'" (x.__class__, x.args, sql), \
sys.exc_info()[2]

This uses the original traceback object (sys.exc_info()[2]), but raises it
with your OperationalError and error string, which can obviously include
object values not available at the actual point where the original exception
was raised. You could obviously include the first two values from
sys.exc_info() in your new exception as well.

Skip

Jul 18 '05 #3
George Young <gr*@ll.mit.edu> writes:
[python 2.3.2, SuSE Linux 8.2, x86] [...] try:
stuff
except _pg.error, msg:
raise DatabaseError, "error '%s' in '%s'" % ( msg, sql )
except:
raise OperationalError, "internal error in '%s'" % sql
This accomplishes passing useful info, namely "sql", on with
the exception. Unfortunately it *loses* info in that upstream
has no way to know *which* exception triggered this or what args
it might have been given. I'm trying to think of a clean way
to improve this. I could do:


Just have an attribute of OperationalError that holds the exception
caught by the except:
John
Jul 18 '05 #4

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:...
9
by: David Stockwell | last post by:
In referring to my copy of the python bible, it tells me I can't use all three items 'try' except and finally. I can use the t/f or t/e combinations though What combination can i use if i want...
14
by: joshsackett | last post by:
I have a WHERE clause that could be an "=" or a "LIKE" depending upon if the passed variable is populated or not. I would like to know the best way to write the WHERE clause to make it dynamically...
1
by: Nick Coghlan | last post by:
Several standard library modules (e.g., cPickle/pickle, cStringIO/StringIO, threading/dummy_threading) have versions which may not be available on all platforms, and pure Python fallbacks that work...
20
by: John Salerno | last post by:
I'm starting out with this: try: if int(text) 0: return True else: self.error_message() return False except ValueError: self.error_message()
2
by: AWasilenko | last post by:
I can't figure out this problem Im having, I just can't understand why it is ignoring the call I put in. First the code (This is a cherrypy website): import sys, cherrypy, html class Root:...
5
by: Robert Hicks | last post by:
Is it good practice to do something like: try: f1 = file('file1') f2 = file('file2') except: # catch the exception Or do you do a try/except for each open?
9
by: =?Utf-8?B?VHlsZXIgUy4=?= | last post by:
I have VS.Net with SQL Server 2005 and I'm developing a windows application in C# and I'm trying to write a query to use an EXCEPT clause and I'm getting an error saying the EXCEPT clause isn't...
11
by: cnb | last post by:
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?...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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...

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.