Raising an exception in the caller's frame

Jp Calderone exarkun at intarweb.us
Fri Mar 21 00:46:38 CET 2003

On Thu, Mar 20, 2003 at 09:14:22PM +0100, Thomas Heller wrote:
> Is there any way to raise an exception in the callers (parent) frame?

  I don't believe so (at least not without performing unspeakable evil).

> See the following code, I want the exception to be raised in the 
> do_some_work function, without changing this function itself:
> def check_result(value):
>     if somecondition(value):
>         raise ValueError
>     return value
> def do_some_work():
>     value = dosomething()
>     return check_result(value)

  Consider this alternate definition of do_some_work...

    def do_some_work():
        value = dosomething()
            return check_result(value)
        except Exception, e:
            raise e

  I'm not sure I'd use this, though - it masks useful information about the
real source of the exception.  If I might ask, why do you want to do this?


"One World, one Web, one Program." - Microsoft(R) promotional ad
"Ein Volk, ein Reich, ein Fuhrer." - Adolf Hitler
 up 19:58, 4 users, load average: 0.06, 0.04, 0.00

More information about the Python-list mailing list