raise errors

D

daved170

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
 
U

Ulrich Eckhardt

daved170 said:
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
 
P

Peter Otten

daved170 said:
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:
.... try:
.... oops
.... except:
.... traceback.print_exc()
.... raise
........ try:
.... foo()
.... except:
.... traceback.print_exc()
....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
 
B

Benjamin Kaplan

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
 
G

Gabriel Genellina

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top