raise errors

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Sep 26 11:17:22 CEST 2009


En Mon, 21 Sep 2009 06:17:20 -0300, daved170 <daved170 at gmail.com> escribió:

> I need help with exceptions raising.
> My goal is to print at the outer functions all the errors including
> the most inner one.

The default exception report contains a lot of information, you don't have  
to do anything special.

> def foo1(self):
>    try:
>         foo2()
>    except ? :
>          print "outer Err at foo1" + ??
>
> def foo2(self):
>    try:
>         error occured
>    except ? :
>          raise "inner Err at foo2"

The bare bones structure is:

def foo1():
   foo2()

def foo2():
   undefinedname()

foo1()

and you get this error:

D:\temp>python test638580.py
Traceback (most recent call last):
   File "test638580.py", line 7, in <module>
     foo1()
   File "test638580.py", line 2, in foo1
     foo2()
   File "test638580.py", line 5, in foo2
     undefinedname()
NameError: global name 'undefinedname' is not defined


> I would like the print to be : outer Err at foo1 , inner Err at foo1

It already includes that info.

   File "test638580.py", line 2, in foo1
     foo2()

means that in line 2 of test638580.py, inside function foo1, there was a  
call to foo2.

   File "test638580.py", line 5, in foo2
     undefinedname()

and that means that foo2 tried to call undefinedname.

   NameError: global name 'undefinedname' is not defined

And that's the actual error.

Do you want it to be more nicely formatted? Do you want to include  
additional information? Or what?

-- 
Gabriel Genellina




More information about the Python-list mailing list