Catching error text like that shown in console?

Discussion in 'Python' started by Peter A. Schott, Dec 9, 2005.

  1. I know there's got to be an easy way to do this - I want a way to catch the
    error text that would normally be shown in an interactive session and put that
    value into a string I can use later. I've tried just using a catch statement
    and trying to convert the output to string, but this doesn't always work. I
    really don't have programs complex enough to do a lot of specific catching at
    this point - I just want to know:
    1. something failed
    2. here's the error output/traceback

    Anyone know how I can do this or if it's possible? Didn't find anything doing a
    search so I figured I'd hit up the experts.

    Thanks.

    -Pete Schott
    Peter A. Schott, Dec 9, 2005
    #1
    1. Advertising

  2. Peter A. Schott

    gene tani Guest

    Peter A. Schott wrote:
    > I know there's got to be an easy way to do this - I want a way to catch the
    > error text that would normally be shown in an interactive session and put that
    > value into a string I can use later. I've tried just using a catch statement
    > and trying to convert the output to string, but this doesn't always work. I
    > really don't have programs complex enough to do a lot of specific catching at
    > this point - I just want to know:
    > 1. something failed
    > 2. here's the error output/traceback
    >
    > Anyone know how I can do this or if it's possible? Didn't find anything doing a
    > search so I figured I'd hit up the experts.
    >
    > Thanks.
    >
    > -Pete Schott


    I don't have an exact answer for you, but since no one else replied:

    look at subprocess module, and os.popen4()

    http://docs.python.org/lib/os-newstreams.html#os-newstreams
    http://docs.python.org/lib/module-subprocess.html
    http://mail.python.org/pipermail/python-list/2005-October/305671.html

    Hint: mentioning your O/S is pretty essential; for many issues, what
    version of py and how you installed it also
    gene tani, Dec 9, 2005
    #2
    1. Advertising

  3. Peter A. Schott

    Larry Bates Guest

    Peter A. Schott wrote:
    > I know there's got to be an easy way to do this - I want a way to catch the
    > error text that would normally be shown in an interactive session and put that
    > value into a string I can use later. I've tried just using a catch statement
    > and trying to convert the output to string, but this doesn't always work. I
    > really don't have programs complex enough to do a lot of specific catching at
    > this point - I just want to know:
    > 1. something failed
    > 2. here's the error output/traceback
    >
    > Anyone know how I can do this or if it's possible? Didn't find anything doing a
    > search so I figured I'd hit up the experts.
    >
    > Thanks.
    >
    > -Pete Schott


    1. Create a function to be run when an exception happens.

    def excepthook(self, type, value, tb):
    #
    # This function allows the user to redefine what happens if the program
    # aborts due to an uncaught exception.
    import traceback
    #
    # Get traceback lines and append the current session log
    #
    tblines=traceback.format_exception(type, value, tb)
    #
    # Print these lines somewhere. Normally you would log to a file
    #
    print "traceback exception encountered"
    print "--------------------Begin traceback lines-------------------------"
    print tblines
    print "--------------------End traceback lines---------------------------")
    map(sys.stdout.writelines, tblines) # Always write exceptions to screen
    sys.exit(2)

    2. Tell python you want to call this function when exception occurs.

    sys.excepthook=exceptHandler
    Larry Bates, Dec 10, 2005
    #3
  4. Peter A. Schott

    Kent Johnson Guest

    Peter A. Schott wrote:
    > I know there's got to be an easy way to do this - I want a way to catch the
    > error text that would normally be shown in an interactive session and put that
    > value into a string I can use later. I've tried just using a catch statement
    > and trying to convert the output to string, but this doesn't always work. I
    > really don't have programs complex enough to do a lot of specific catching at
    > this point - I just want to know:
    > 1. something failed
    > 2. here's the error output/traceback


    If you just want to catch exceptions and print a traceback, use the
    traceback module. This is handy for example if you are processing a
    number of items and don't want a failure in one to abort the whole loop:

    import traceback
    for work in thingsToDo:
    try:
    doSomeWork(work)
    except:
    traceback.print_exc()

    If you want more control over the exception info - for example to put it
    in a string instead of printing it - look at the other functions in the
    traceback module.

    Kent
    Kent Johnson, Dec 10, 2005
    #4
    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. IchBin
    Replies:
    3
    Views:
    1,279
    IchBin
    Mar 2, 2005
  2. Byrom, R (Rob)

    Catching console output interactively

    Byrom, R (Rob), Apr 16, 2004, in forum: Python
    Replies:
    4
    Views:
    333
    Michael Hudson
    Apr 20, 2004
  3. Patrick Kowalzick
    Replies:
    5
    Views:
    470
    Patrick Kowalzick
    Mar 14, 2006
  4. Mete Akalýn
    Replies:
    1
    Views:
    408
    Mete Akalýn
    Jul 25, 2003
  5. Or
    Replies:
    4
    Views:
    113
Loading...

Share This Page