Re: raise errors
En Mon, 21 Sep 2009 06:17:20 -0300, daved170 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 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: raise errors
On Mon, Sep 21, 2009 at 5:17 AM, daved170 wrote: > Hi everybody, > 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 > > thanks > daved > -- First of all, what version of Python are you using? String exceptions are deprecated in Python 2.5 and removed in 2.6. You should switch to using Exceptions. In particular, you'll probably want to subclass Exception. String exceptions use the syntax "except 'exception string':" which means you need to know the string ahead of time. You'll instead want to do this. def foo1() : try : foo2() except Exception, e : print "outer exception at foo1, %s" % str(e) def foo2() : raise Exception("inner exception at foo2") In Python 3.x, the syntax changed to "except Exception as e" but that syntax isn't available in versions prior to 2.6 > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: raise errors
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 "", line 3, in foo NameError: global name 'oops' is not defined Traceback (most recent call last): File "", line 3, in bar File "", line 3, in foo NameError: global name 'oops' is not defined Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: raise errors
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 Take a look at the builtin debugger facility, i.e. 'import pdb'. There, you have a traceback module which might already give you what you want. One more advise though: Do not catch exceptions you don't handle! Make sure they can pass through your code without hurting anything and then catch and log them in a place where you actually can handle them. In particular, don't wrap each and every tiny morsel of code into try/except handlers as you would (have to) do if you were using return values. Uli -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 -- http://mail.python.org/mailman/listinfo/python-list