raise errors

Peter Otten __peter__ at web.de
Mon Sep 21 12:47:52 CEST 2009


daved170 wrote:

> I need help with exceptions raising.
> My goal is to print at the outer functions all the errors including
> the most inner one.
> 
> For example:
> 
> def foo1(self):
>    try:
>         foo2()
>    except ? :
>          print "outer Err at foo1" + ??
> 
> def foo2(self):
>    try:
>         error occured
>    except ? :
>          raise "inner Err at foo2"
> 
> 
> the ? remarks that I have no idea what to use.
> 
> I would like the print to be : outer Err at foo1 , inner Err at foo1

Have a look at the traceback module. Example:

>>> def foo():
...     try:
...             oops
...     except:
...             traceback.print_exc()
...             raise
...
>>> def bar():
...     try:
...             foo()
...     except:
...             traceback.print_exc()
...
>>> import traceback
>>> bar()
Traceback (most recent call last):
  File "<stdin>", line 3, in foo
NameError: global name 'oops' is not defined
Traceback (most recent call last):
  File "<stdin>", line 3, in bar
  File "<stdin>", line 3, in foo
NameError: global name 'oops' is not defined

Peter




More information about the Python-list mailing list