Capturing exceptions?

Discussion in 'Python' started by Dfenestr8, Aug 25, 2004.

  1. Dfenestr8

    Dfenestr8 Guest

    Hi.

    What's the easiest way to capture the traceback from an exception, and
    write it to a file? Ideally, I'd like to be able to do something like:

    >try:
    > main()
    >except:
    > write the traceback to a file
     
    Dfenestr8, Aug 25, 2004
    #1
    1. Advertising

  2. Dfenestr8 wrote:
    > What's the easiest way to capture the traceback from an exception,
    > and write it to a file? Ideally, I'd like to be able to do
    > something like:
    >
    >>try:
    >>main()
    >>except:
    >>write the traceback to a file


    Have a look at the traceback module.

    Mathias
     
    Mathias Waack, Aug 25, 2004
    #2
    1. Advertising

  3. Dfenestr8 <> writes:

    > Hi.
    >
    > What's the easiest way to capture the traceback from an exception, and
    > write it to a file? Ideally, I'd like to be able to do something like:
    >
    > >try:
    > > main()
    > >except:
    > > write the traceback to a file


    Read up on the traceback module.

    Cheers,
    mwh

    --
    6. Symmetry is a complexity-reducing concept (co-routines include
    subroutines); seek it everywhere.
    -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html
     
    Michael Hudson, Aug 25, 2004
    #3
  4. Dfenestr8

    Larry Bates Guest

    You need to set up an exception hook function, register
    it with Python and have it write out the traceback
    information to your file.

    def excepthook(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)
    #
    # Insert code to write to file here
    #
    f=open('traceback.log', 'a')
    f.writelines(tblines)
    f.close()
    sys.exit(0)

    Then in main program

    #
    # Set the sys.excepthook so I can clean up properly if main program
    aborts
    #
    sys.excepthook=excepthook




    "Dfenestr8" <> wrote in message
    news:p...
    > Hi.
    >
    > What's the easiest way to capture the traceback from an exception, and
    > write it to a file? Ideally, I'd like to be able to do something like:
    >
    > >try:
    > > main()
    > >except:
    > > write the traceback to a file
     
    Larry Bates, Aug 25, 2004
    #4
  5. Dfenestr8

    Dfenestr8 Guest

    On Wed, 25 Aug 2004 19:19:06 +0200, Mathias Waack wrote:

    > Dfenestr8 wrote:
    >> What's the easiest way to capture the traceback from an exception, and
    >> write it to a file? Ideally, I'd like to be able to do something like:
    >>
    >>>try:
    >>>main()
    >>>except:
    >>>write the traceback to a file

    >
    > Have a look at the traceback module.
    >
    > Mathias


    I did. Thanx. But I'm afraid I don't understand it.

    From looking at the docs, it would appear the method extract.tb(traceback,
    limit) is the method I want. But it appears to want some kind of object as
    its parameter, and I have no idea what kind of object that is.

    I've tried "a = traceback.extract_tb(tb)",
    "traceback.extract_tb(traceback)", "traceback.extract_tb(sys.exc_info())".

    What kind of parameter does it need?
     
    Dfenestr8, Aug 25, 2004
    #5
  6. Dfenestr8 wrote:

    > On Wed, 25 Aug 2004 19:19:06 +0200, Mathias Waack wrote:
    >
    >> Dfenestr8 wrote:
    >>> What's the easiest way to capture the traceback from an
    >>> exception, and write it to a file? Ideally, I'd like to be able
    >>> to do something like:
    >>>
    >>>>try:
    >>>>main()
    >>>>except:
    >>>>write the traceback to a file

    >>
    >> Have a look at the traceback module.
    >>
    >> Mathias

    >
    > I did. Thanx. But I'm afraid I don't understand it.


    import traceback
    try: raise Exception, "python is so cool;)"
    except: traceback.print_exc(None,file("exc.trc","w"))

    Does it that what you want?

    Mathias
     
    Mathias Waack, Aug 25, 2004
    #6
  7. Dfenestr8

    Dfenestr8 Guest

    On Wed, 25 Aug 2004 20:55:31 +0200, Mathias Waack wrote:

    >> On Wed, 25 Aug 2004 19:19:06 +0200, Mathias Waack wrote:
    >>
    >>> Dfenestr8 wrote:
    >>>> What's the easiest way to capture the traceback from an exception, and
    >>>> write it to a file? Ideally, I'd like to be able to do something like:
    >>>>
    >>>>>try:
    >>>>>main()
    >>>>>except:
    >>>>>write the traceback to a file
    >>>
    >>> Have a look at the traceback module.
    >>>
    >>> Mathias

    >>
    >> I did. Thanx. But I'm afraid I don't understand it.

    >
    > import traceback
    > try: raise Exception, "python is so cool;)" except:
    > traceback.print_exc(None,file("exc.trc","w"))
    >
    > Does it that what you want?
    >
    > Mathias


    yep. Should do.

    Thanx.

    PS: Still would like to know what kind of parameter that other method
    wants.
     
    Dfenestr8, Aug 25, 2004
    #7
    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. Charles Shannon Hendrix

    REGEX: capturing on optional groups which fail

    Charles Shannon Hendrix, Jun 14, 2004, in forum: Perl
    Replies:
    0
    Views:
    554
    Charles Shannon Hendrix
    Jun 14, 2004
  2. Ahmed Moustafa
    Replies:
    5
    Views:
    30,035
    Chris Smith
    Jul 14, 2004
  3. Paul Miller
    Replies:
    3
    Views:
    1,020
    Alex Martelli
    Nov 12, 2003
  4. Replies:
    3
    Views:
    613
    Sherm Pendley
    Apr 16, 2007
  5. Lie
    Replies:
    3
    Views:
    635
Loading...

Share This Page