Displaying error message in a try except?

Discussion in 'Python' started by wawork@hotmail.com, Dec 11, 2005.

  1. Guest

    Fairly new to python. In a try except how do you display the true
    (raw) error message so it can be displayed back to the user?
    , Dec 11, 2005
    #1
    1. Advertising

  2. wrote:

    > Fairly new to python. In a try except how do you display the true
    > (raw) error message so it can be displayed back to the user?


    assuming that "true" means "the message you would get if you hadn't
    used a try/except", the traceback module is what you want:

    import traceback

    try:
    raise SyntaxError("example")
    except:
    traceback.print_exc()

    prints

    Traceback (innermost last):
    File "module.py", line 4, in ?
    SyntaxError: example

    more here:

    http://effbot.org/librarybook/traceback.htm

    you can also inspect the exception status via the sys.exc_info() call.
    e.g.

    import sys

    try:
    1/0
    except:
    print "%s: %s" % sys.exc_info()[:2]

    prints

    exceptions.ZeroDivisionError: integer division or modulo by zero

    for more info, see the library reference.

    hope this helps!

    </F>
    Fredrik Lundh, Dec 11, 2005
    #2
    1. Advertising

  3. Xavier Morel Guest

    Fredrik Lundh wrote:
    > assuming that "true" means "the message you would get if you hadn't
    > used a try/except", the traceback module is what you want:
    >
    > you can also inspect the exception status via the sys.exc_info() call.
    > e.g.
    >

    There is also the third way of catching an exception explicitly and
    printing it's arguments and class (doesn't give exactly the same
    information, but gives relevant informations nonetheless)

    --
    >>> try:

    1/0
    except ZeroDivisionError, e:
    print e
    print e.args
    print repr(e)


    integer division or modulo by zero
    ('integer division or modulo by zero',)
    <exceptions.ZeroDivisionError instance at 0x00E35850>
    --

    (catching Exception instead of ZeroDivisionError would yield the same
    result, but would also act as an Exception trap that voids any exception
    raised)
    Xavier Morel, Dec 11, 2005
    #3
    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. John Salerno
    Replies:
    20
    Views:
    809
    John Salerno
    Aug 11, 2006
  2. Fabio Z Tessitore

    who is simpler? try/except/else or try/except

    Fabio Z Tessitore, Aug 12, 2007, in forum: Python
    Replies:
    5
    Views:
    356
  3. David House

    try -> except -> else -> except?

    David House, Jul 6, 2009, in forum: Python
    Replies:
    2
    Views:
    315
    Bruno Desthuilliers
    Jul 6, 2009
  4. MRAB
    Replies:
    4
    Views:
    294
  5. Peng Yu
    Replies:
    1
    Views:
    494
    Steven D'Aprano
    Nov 18, 2009
Loading...

Share This Page