473,398 Members | 2,212 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,398 software developers and data experts.

preserve exception within try .. finally

Hello;

I'm curious to know how people preserve exceptions that arise in a try
... finally block. Consider this example:

try:
getResource()
doSomething()
finally:
alwaysFreeResource()

If an exception occurs in doSomething(), the resource is freed anyway --
which is good. How can I keep the exception 'raised' for another
try-finally/except to deal with? Does this problem reflect an error in
the way I am approaching the problem?
Many thanks,

Brian.

Jul 18 '05 #1
4 2929
On Fri, 10 Oct 2003 15:57:13 -0400, Brian Alexander
<br******@sympatico.ca> wrote:
Hello;

I'm curious to know how people preserve exceptions that arise in a try
.. finally block. Consider this example:

try:
getResource()
doSomething()
finally:
alwaysFreeResource()

If an exception occurs in doSomething(), the resource is freed anyway --
which is good. How can I keep the exception 'raised' for another
try-finally/except to deal with? Does this problem reflect an error in
the way I am approaching the problem?


Maybe I'm not understanding your question but the finally block does
*not* handle the exception. The exception propagates:
try: .... raise Exception
.... finally:
.... print "I warned you"
....
I warned you
Traceback (most recent call last):
File "<interactive input>", line 2, in ?
Exception


HTH,
G. Rodrigues
Jul 18 '05 #2
Em Sex 10 Out 2003 16:57, Brian Alexander escreveu:
Hello;

I'm curious to know how people preserve exceptions that arise
in a try .. finally block. Consider this example:

try:
getResource()
doSomething()
finally:
alwaysFreeResource()

If an exception occurs in doSomething(), the resource is freed
anyway -- which is good. How can I keep the exception 'raised'
for another try-finally/except to deal with? Does this problem
reflect an error in the way I am approaching the problem?
Many thanks,

Brian.


I would do:

getResource()
try:
doSomething()
finally:
freeResource()

The exception keeps propagating until it is handled by a try:
except: statement.

--
Alvaro Figueiredo
al*****@freeshell.org

Jul 18 '05 #3
Brian Alexander wrote:
I'm curious to know how people preserve exceptions that arise in a try
.. finally block. Consider this example:

try:
getResource()
doSomething()
finally:
alwaysFreeResource()

If an exception occurs in doSomething(), the resource is freed anyway --
which is good. How can I keep the exception 'raised' for another
try-finally/except to deal with? Does this problem reflect an error in
the way I am approaching the problem?


The exception raised by doSomething() travels all the way up the call stack
until it hits an except clause that can catch it, i. e.

except ExceptionRaised:
# handle it

or

except SuperClassOfExceptionRaised:
# handle it

All the finally blocks that lie on the way up are executed.
Note that the correct use of try... finally is

getResource() # don't free if it cannot be acquired
try:
doSomething()
finally
freeResource()

This will typically be included (not necessarily in the same function) by a
try ... except

try:
getResource()
try:
doSomething()
finally
freeResource()
except SomeException, e:
if canHandleIt(e):
#handle it
else:
raise # hope for another handler

I think that the canHandleIt(e) test and the reraising is not very common,
but I included it as you were asking for "keeping it raised", which might
translate into "reraising it".

Peter



Jul 18 '05 #4
On Fri, 10 Oct 2003 15:57:13 -0400, Brian Alexander
<br******@sympatico.ca> wrote:
Hello;

I'm curious to know how people preserve exceptions that arise in a try
.. finally block. Consider this example:

try:
getResource()
doSomething()
finally:
alwaysFreeResource()
I would typically go with...

try:
getResource()
doSomething()
freeResource()

except ResourceAllocationFailed :
cleanupWithoutFreeResource ()
raise # re-raise same exception

except :
cleanupWithoutFreeResource ()
freeResource ()
raise # re-raise same exception
The reason being that you don't want to free a resource if you
couldn't allocate it in the first place - cleanup actions often depend
of what exact exception occurred.

However, if you write your cleanup code to handle this, using a
'finally' is normally cleaner.
If an exception occurs in doSomething(), the resource is freed anyway --
which is good. How can I keep the exception 'raised' for another
try-finally/except to deal with? Does this problem reflect an error in
the way I am approaching the problem?


I'm pretty sure you don't need to. If the 'finally' block was entered
due to an exception, the exception is automatically reraised at the
end. If the 'finally' block is entered because the 'try' block was
exhausted, no exception is raised. This will normally do what you want
automatically.

For example...
try :

.... try :
.... raise ErrorName
.... finally :
.... print "Finally 1"
.... print "Hello"
.... finally :
.... print "Finally 2"
....
Finally 1
Finally 2
Traceback (most recent call last):
File "<stdin>", line 3, in ?
NameError: name 'ErrorName' is not defined
The "Hello" is not printed because the inner finally block reraises
the exception. The outer finally block does the same, so the exception
propogates all the way out to trigger a traceback. Obviously from the
traceback given, I didn't bother declaring 'ErrorName' ;-)
--
Steve Horne

steve at ninereeds dot fsnet dot co dot uk
Jul 18 '05 #5

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

Similar topics

5
by: Kevin Jackson | last post by:
In the following code snippet, will the finally block be executed when the throw is executed in the catch block???? I'm assuming it will. catch (Exception e) { // if...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
4
by: Rob Richardson | last post by:
Greetings! I am working on an application that targets a Pocket PC running Windows CE and SQL Server CE. Almost all functions in the application use a Try block with a Catch block that looks...
5
by: Simon Tamman {Uchiha Jax} | last post by:
Now this is bugging me. I just released software for a client and they have reported an unhandled stack overflow exception. My first concern is that the entirity of the UI and any threaded...
4
by: garyusenet | last post by:
Hi I'm using the following code which is finally working. Public Class Form1 Shared ActElement As Object Shared ActFields As DataSet Public Sub SetActElement() Dim objApp As New Object
3
by: matko | last post by:
This is a long one, so I'll summarize: 1. What are your opinions on raising an exception within the constructor of a (custom) exception? 2. How do -you- validate arguments in your own...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
8
by: ms news group | last post by:
What happens if exception is thown within a fixed block? Will the pinned memory buffer get unpinned? and if the pinning pointer points to a managed memery buffer allocated within the throwing...
9
by: TC | last post by:
Hey All, I posted this to the Crypto users group and forgot to add the VB.Net users group. I apologize for any confusion. I have been testing a try / catch / finally block and purposely...
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: 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?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.