473,396 Members | 1,799 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.

re-entering in the normal flow after an exception is raised

I think I want "re-entrant exceptions" (not sure if this is the
correct name).

Consider the following example:

class NotSeriousException(Exception):
pass

def do_this():
raise NotSeriousException()

def do_that():
pass

def do_this_and_that():
do_this()
do_that()

Since the exception is raised at the do_this() level, do_that() will
not be executed. However, since the exception is not that serious, I
would like to catch it and continue from the point the exception was
raised, i.e. something like that:

try:
do_this_and_that()
except NotSeriousException:
continue # does not work of course

where "continue" would continue from do_that().

Is there some elegant or hackish way to get this?
Notice that I do NOT want to modify the source code of
do_this_and_that.
It comes from real life code and I could modify it, but if there was
some
smarter way of getting what I want I would be interested ...

Michele Simionato
Jul 18 '05 #1
7 1666
Michele Simionato wrote:
try:
do_this_and_that()
except NotSeriousException:
continue # does not work of course

where "continue" would continue from do_that().

Is there some elegant or hackish way to get this?
Notice that I do NOT want to modify the source code of
do_this_and_that.


Maybe you could extract the function from the current scope:

def do_this():
raise KeyError

def do_that():
return 123

def do_this_and_that():
do_this()
do_that()

try:
do_this_and_that()
except KeyError:
my_do_that = globals()['do_that']
print my_do_that()
Istvan.
Jul 18 '05 #2
Istvan Albert wrote:
Maybe you could extract the function from the current scope:


heh ... what a stupid circular logic ... since then
it would be directly available anyway ... sorry ...
Jul 18 '05 #3
Michele Simionato <mi***************@gmail.com> wrote:
I think I want "re-entrant exceptions" (not sure if this is the
correct name).
"resumable" is more common, I believe.
Is there some elegant or hackish way to get this?


I think not. You may read in Stroustrup's book about C++'s design and
evolution how he was convinced to abandon resumable exceptions by
experiences in another language, where they just didn't pay for their
huge complexity -- they seem a good idea, but don't prove to be...
they're not used often enough to justify their complexity, in practice
(designing your software well enough that exceptions do not _need_
resumption isn't that big a deal).

If something is too complicated for C++, I would assume it's WAY too
complicated for Python. So, I'm pretty happy Python's exceptions (like
C++'s, Java's, C#'s, ...) are non-resumable!
Alex
Jul 18 '05 #4
Alex Martelli wrote:
If something is too complicated for C++, I would assume it's WAY
too
complicated for Python.


It's too complicated for C++ to handle lists containing arbitrary
types, declaring functions working on arbitrary types, creating
classes at runtime...

SCNR
Mathias
Jul 18 '05 #5
>>>>> "Alex" == Alex Martelli <al*****@yahoo.com> writes:

Alex> Michele Simionato <mi***************@gmail.com> wrote:
I think I want "re-entrant exceptions" (not sure if this is the Is there some elegant or hackish way to get this?


Alex> I think not. You may read in Stroustrup's book about C++'s
Alex> design and evolution how he was convinced to abandon
Alex> resumable exceptions by experiences in another language,
Alex> where they just didn't pay for their huge complexity -- they
Alex> seem a good idea, but don't prove to be...

In Python they would probably be much less complex - exceptions are a
big overhead in C++ already, resumable ones would be even more so. In
python the implementation probably wouldn't be that complex and the
overhead that large.

Also, I can't help but think of the recently posted PEP 334 (Simple
Coroutines via SuspendIteration) at

http://www.python.org/peps/pep-0334.html

I probably wouldn't use resumable exceptions for normal situations
where exceptions are used, but some nice control flow hacks could be
made possible.

--
Ville Vainio http://tinyurl.com/2prnb
Jul 18 '05 #6
On 28 Sep 2004 09:17:07 -0700, mi***************@gmail.com (Michele Simionato) wrote:
I think I want "re-entrant exceptions" (not sure if this is the
correct name). As Alex said, resumable is the usual word, I think.
Consider the following example:

class NotSeriousException(Exception):
pass

def do_this():
raise NotSeriousException()

def do_that():
pass

def do_this_and_that():
do_this()
do_that()

Since the exception is raised at the do_this() level, do_that() will
not be executed. However, since the exception is not that serious, I
would like to catch it and continue from the point the exception was
raised, i.e. something like that:

try:
do_this_and_that()
except NotSeriousException:
continue # does not work of course

where "continue" would continue from do_that().

Is there some elegant or hackish way to get this? Hacks will probably bite you later, though I think they're fine
for one-time throwaway code that you really throw away.

I wonder if you could interfere with exception unwinding via sys.settrace.
I believe you can get control at each frame as the exception propagates,
but I don't know what can be done. I think you would take a big performance hit though.
Notice that I do NOT want to modify the source code of
do_this_and_that.
It comes from real life code and I could modify it, but if there was
some
smarter way of getting what I want I would be interested ...

Maybe think about smart ways to modify?

It comes to mind that you might have functions call a complain function
instead of directly raising exceptions, if you want to customize or parameterize
by seriousness or other criteria. E.g., (untested)

def complain(kind, exc, *args):
if kind in globalconfigdict: raise exc(*args)

def dothis():
complain('not so serious', NotSeriousException, 'args', 'for', 'exception')

Just an idea. You could also make complain a callable object with various methods for
configuring behaviour easily etc. etc. Just .02USD off the top of my head ;-)

Regards,
Bengt Richter
Jul 18 '05 #7
Mathias Waack <M.*****@gmx.de> wrote:
Alex Martelli wrote:
If something is too complicated for C++, I would assume it's WAY
too
complicated for Python.


It's too complicated for C++ to handle lists containing arbitrary
types, declaring functions working on arbitrary types, creating
classes at runtime...


Runtime creation, yes (due to C++'s choice to work with existing system
tools, chiefly the linker, and limit the language so as to make it
reasonably easy to implement on top of existing linkers) -- the other
stuff, minus the runtime part, is easily handled by templates, of
course, so it's _not_ "too complicated for C++". If you relax the
constraint on linker limitations (like just about all existing C++
implementations do), then creating and dynamically loading dynamic
libraries allows the do-it-at-runtime part just as well.

Resumable exceptions are a completely different issue from "what
capabilities can I assume in the existing system tools that I want to be
able to collaborate with".
Alex
Jul 18 '05 #8

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

Similar topics

1
by: Nel | last post by:
I have a question related to the "security" issues posed by Globals ON. It is good programming technique IMO to initialise variables, even if it's just $foo = 0; $bar = ""; Surely it would...
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"; }
1
by: James | last post by:
Hi, I would like to run a custom script on a linux box via a button on a php page (php webpage hosted on the same linux box). Is this possible? If so , can you give me a pointer in the right...
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...
10
by: James | last post by:
What is the best method for creating a Web Page that uses both PHP and HTML ? <HTML> BLA BLA BLA BLA BLA
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...
3
by: David Hickman | last post by:
Hi all, This is a newbie question but I have been struggling with this for ages. I have a document with 2 separate <?php sections of code. I would like to declare some variables in the first...
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
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
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
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...
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,...

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.