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

Istvan Albert ialbert at mailblocks.com
Tue Sep 28 13:38:07 EDT 2004


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.



More information about the Python-list mailing list