raise errors

Discussion in 'Python' started by daved170, Sep 21, 2009.

  1. daved170

    daved170 Guest

    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
     
    daved170, Sep 21, 2009
    #1
    1. Advertising

  2. 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
     
    Ulrich Eckhardt, Sep 21, 2009
    #2
    1. Advertising

  3. daved170

    Peter Otten Guest

    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
     
    Peter Otten, Sep 21, 2009
    #3
  4. 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
    >
     
    Benjamin Kaplan, Sep 21, 2009
    #4
  5. 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 <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
     
    Gabriel Genellina, Sep 26, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,031
    Mark Goldin
    Jan 17, 2004
  2. Jacol

    raise or not to raise [Newbie]

    Jacol, Feb 3, 2007, in forum: Python
    Replies:
    5
    Views:
    437
    Gabriel Genellina
    Feb 5, 2007
  3. ernest
    Replies:
    2
    Views:
    311
    Roy Smith
    Nov 14, 2010
  4. Jack Bates
    Replies:
    0
    Views:
    297
    Jack Bates
    May 2, 2011
  5. bvdp

    Raise X or Raise X()?

    bvdp, Mar 11, 2012, in forum: Python
    Replies:
    10
    Views:
    408
    Stefan Behnel
    Mar 12, 2012
Loading...

Share This Page