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

Re-raising the right exception

SM
try:
x = 1/0
except ZeroDivisionError:
print "caught ZeroDivisionError"
try:
name_error
except NameError:
print "caught NameError"
raise

According to the Python Language Reference
http://docs.python.org/ref/raise.html
'raise' re-raises the last expression that was active in the current
scope. Here apparently "scope" means some sort of lexical scope,
because this program terminates with a NameError exception and not a
ZeroDivisionError.

It seems useful to have a way to raise the exception that is currently
being handled (ZeroDivisionError in this case) and not any exception
that happened to be caught earlier in the current exception handler.
For example,

try:
network.command()
except:
try:
network.close()
except:
pass
raise

I want to make a network call, and if it raises an exception, I want
to close the network connection before allowing the exception to
propagate up the stack. However, trying to close the network
connection might itself raise an exception, and I don't care about
that exception; I want to send the original exception. Apparently the
way to do this is:

try:
network.command()
except Exception, bob:
try:
network.close()
except:
pass
raise bob
Jul 18 '05 #1
1 1233
SM wrote:
For example,

try:
network.command()
except:
try:
network.close()
except:
pass
raise

I want to make a network call, and if it raises an exception, I want
to close the network connection before allowing the exception to
propagate up the stack. However, trying to close the network
connection might itself raise an exception, and I don't care about
that exception; I want to send the original exception. Apparently the
way to do this is:


<snip>

You could move the cleanup code out to another scope:

try:
network.command()
except:
def network_cleanup():
try:
network.close()
except:
pass

network_cleanup()
raise

In practice something like network_cleanup probably wants to be called from
more than one place so it could be in a function defined elsewhere.

Another option is to use try..finally:

try:
network.command()
finally:
try:
network.close()
except:
pass

then you don't need to reraise the exception at all.

Use the first suggestion if the cleanup only needs to be done when the code
failed (e.g. to remove partially created files from disc). Use the second
if the cleanup needs to be done always, e.g. closing files or network
connections.
Jul 18 '05 #2

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

Similar topics

4
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as...
1
by: Chris | last post by:
Sorry to post so much code all at once but I'm banging my head against the wall trying to get this to work! Does anyone have any idea where I'm going wrong? Thanks in advance and sorry again...
11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
4
by: Alan Walkington | last post by:
Folks: How can I get an /exec'ed/ process to run in the background on an XP box? I have a monitor-like process which I am starting as 'exec("something.exe");' and, of course the exec function...
1
by: John Ryan | last post by:
What PHP code would I use to check if submitted sites to my directory actually exist?? I want to use something that can return the server code to me, ie HTTP 300 OK, or whatever. Can I do this with...
8
by: Beowulf | last post by:
Hi Guru's, I have a query regarding using PHP to maintain a user profiles list. I want to be able to have a form where users can fill in their profile info (Name, hobbies etc) and attach an...
8
by: Lothar Scholz | last post by:
Because PHP5 does not include the mysql extension any more is there a chance that we will see more Providers offering webspace with Firebird or Postgres Databases ? What is your opinion ? I must...
1
by: joost | last post by:
Hello, I'm kind of new to mySQL but more used to Sybase/PHP What is illegal about this query or can i not use combined query's in mySQL? DELETE FROM manufacturers WHERE manufacturers_id ...
3
by: presspley | last post by:
I have bought the book on advanced dreamweaver and PHP recently. I have installed MySQL and PHP server but am getting an error on the $GET statement show below. It says there is a problem with...
2
by: sky2070 | last post by:
i have two file with jobapp.html calling jobapp_action.php <HTML> <!-- jobapp.html --> <BODY> <H1>Phop's Bicycles Job Application</H1> <P>Are you looking for an exciting career in the world of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.