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

_conditionally_ returning to point where exception was raised?

Hi

I'm new to Python and would like to know if the following is possible.

Say I have one lower-level object A and one user-interface object B.
Suppose B.CallingMethod() calls A.CalledMethod(), the latter method
stumbles upon an IO error and raises an exception. The calling method
detects the exception, but needs to get input from the user before
deciding whether A.CalledMethod() should continue being executed or
permantently stop/cancel the execution of the remaining code in
A.CalledMethod(). Can this be done?

What I would like to know is if the calling method is allowed to
"decide" (at run-time) whether or not the execution of the called
method resumes. Until now I only see two possibilities: either the
caller is coded in a way that it handles the exception and execution of
the called method resumes at the point where the exception was raised
or the caller was written in a way that doesn't handle it and the
program is stopped by the interpreter. Is there a third way, ie is it
possible to determine at run-time whether the called method is resumed?

Thanks in advance,

Mack Stevenson

Jul 18 '05 #1
3 1679
There's no "Resume Next" in python. Once you catch an exception, the
only way you can go is forward from that point.

So if B.CallingMethod catches an exception that was raised in
A.CalledMethod, all it could do is try calling A.CalledMethod again, it
can't jump back to the point where the exception was raised. About the
best you could, I think, would be to break A.CalledMethod up into
smaller function and let B.CallingMethod call each one in sequence,
deciding whether or not to continue if an exception is raised in any of
them.

Jul 18 '05 #2
On 16 Mar 2005 09:53:11 -0800, "MackS" <ma***********@hotmail.com> wrote:
Hi

I'm new to Python and would like to know if the following is possible.

Say I have one lower-level object A and one user-interface object B.
Suppose B.CallingMethod() calls A.CalledMethod(), the latter method
stumbles upon an IO error and raises an exception. The calling method
detects the exception, but needs to get input from the user before
deciding whether A.CalledMethod() should continue being executed or
permantently stop/cancel the execution of the remaining code in
A.CalledMethod(). Can this be done?

What I would like to know is if the calling method is allowed to
"decide" (at run-time) whether or not the execution of the called
method resumes. Until now I only see two possibilities: either the
caller is coded in a way that it handles the exception and execution of
the called method resumes at the point where the exception was raised
or the caller was written in a way that doesn't handle it and the
program is stopped by the interpreter. Is there a third way, ie is it
possible to determine at run-time whether the called method is resumed?

Thanks in advance,

Once an exception is raised, the stack gets unwound to the point where
the exception is caught, so there is no way[1] to recover the context required
to "continue" (and eventually "return normally" as if the exception hadn't happened).

One way to preserve the stack is to call a call-back function instead of raising
an exception. Then the call-back could return some information (e.g. user input) to
the problem context to make decisions as to how to continue (or not), or the callback
itself could also raise an exception.

But then the caller has to be able to provide a call-back function one way or another.
If you are designing the whole thing, you can do it, but if you are calling low level
services that you can't change, and which raise exceptions, then you may have some
fancy wrapping to do to create your desired interface, if it is even possible.

[1] Just to note that I know it is not wise to say "no way" on c.l.py, since you never
know what clever people will come up with ;-) Maybe someone will create a way for an
exception to return a continuation object of some kind, that could optionally be called
from the exception handler to effect as-if return to the point of an exception and
continuation from there as if the exception hadn't happened (except maybe some updated
binding in the local context that could be seen by the continuing code, and be set via
the contination-object call). I have a hunch it's possible, but pretty hairy.

Regards,
Bengt Richter
Jul 18 '05 #3
... The calling method detects the exception, but needs to get input
from the user before deciding whether A.CalledMethod() should
continue being executed or permantently stop/cancel the execution of
the remaining code in A.CalledMethod(). Can this be done?
Bengt> Once an exception is raised, the stack gets unwound to the point
Bengt> where the exception is caught, so there is no way[1] to recover
Bengt> the context required to "continue" (and eventually "return
Bengt> normally" as if the exception hadn't happened).

I suspect in the OP's case if A.CalledMethod hasn't gone too far before
encountering an IOError it's quite reasonable to thing that B.CallingMethod
just call A.CalledMethod again if it decides to continue.

I will point out that someone posted some "autoload" code here awhile ago
(who was it?) to try to import missing modules. I use a variant in my
Python startup file:

% python
Python 2.5a0 (#75, Mar 15 2005, 21:55:51)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1671)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
print sin(5) found sin in math module
-0.958924274663 conn = MySQLdb.Connection autoloading MySQLdb conn

<function Connect at 0x136abb0>

After finding the module that needs to be imported, it reexecutes the code
object for the frame where the exception was raised:

exec import_stmt in f_locals, f_globals
exec tb.tb_frame.f_code in f_locals, f_globals

That's not exactly what the OP asked for (it executes the code object from
the beginning, not from the point where the exception was raised with
possibly modified locals and globals), but might provide people with some
ideas. The full autoload module is at

http://manatee.mojam.com/~skip/python/autoload.py

Skip
Jul 18 '05 #4

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

Similar topics

0
by: Wes | last post by:
Hello, I have a C++/JNI program that creates a Java object, populates the members and returns the jobject to the calling Java program. The code compiles and runs cleanly with no exceptions being...
8
by: StepH | last post by:
Hi, I've this module : from optparse import OptionParser import re _version = "P1" def writeVar(f, line):
4
by: beliavsky | last post by:
If a function that normally returns N values raises an exception, what should it return? N values of None seems reasonable to me, so I would write code such as def foo(x): try: # code setting...
10
by: Vinny | last post by:
I have a few floating point questions/issues I wish to ask. An answer or discussion would be nice too :) Any links discussion the subject would be nice too.. 1. How do I generate an underflow...
28
by: dcrespo | last post by:
Hi all, How can I get a raised exception from other thread that is in an imported module? For example: --------------- programA.py ---------------
3
by: mirandacascade | last post by:
Verion of Python: 2.4 O/S: Windows XP ElementTree resides in the c:\python24\lib\site-packages\elementtree\ folder When a string that does not contain well-formed XML is passed as an argument...
4
by: james t kirk | last post by:
I'm writing a wrapper class to handle the line merging and filtering for a log file analysis app The problem I'm running into is that the StopIteration exception raised when the wrapped file...
7
by: Michele Petrazzo | last post by:
Hi list, I have a strange error on my software on win 2k/xp and debian 3.1 with py 2.3.5 / 2.4.1 + twisted + wxpython: python, on a piece of code doesn't raise a KeyError on a dict (that don't...
4
by: Giuseppe Chielli | last post by:
Hi to everyone. I am studying VB.net and I am wondering why this trivial code doesn't raise any exception: Option Strict On Imports System Imports Microsoft.VisualBasic Public Class Form1
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?
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...
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.