Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > raise errors

Reply
Thread Tools

raise errors

 
 
daved170
Guest
Posts: n/a
 
      09-21-2009
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
 
Reply With Quote
 
 
 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      09-21-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


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

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      09-21-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

 
Reply With Quote
 
Benjamin Kaplan
Guest
Posts: n/a
 
      09-21-2009
On Mon, Sep 21, 2009 at 5:17 AM, daved170 <(E-Mail Removed)> 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
>

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      09-26-2009
En Mon, 21 Sep 2009 06:17:20 -0300, daved170 <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Raise X or Raise X()? bvdp Python 10 03-12-2012 04:08 PM
"raise (type, value, traceback)" and "raise type, value, traceback" Jack Bates Python 0 05-02-2011 05:23 PM
raise Exception or raise Exception() ernest Python 2 11-14-2010 08:14 PM
raise or not to raise [Newbie] Jacol Python 5 02-05-2007 11:46 PM
Errors, errors, errors Mark Goldin ASP .Net 2 01-17-2004 08:05 PM



Advertisments