Exceptions: Logging TB and local variables?

Discussion in 'Python' started by allen.fowler, Oct 10, 2007.

  1. allen.fowler

    allen.fowler Guest

    Hi,

    My code looks like this:

    for item in bigset:
    self.__sub1(item)
    self.__sub2(item)
    self.__sub3(item)

    # the subX functions, in turn, use various 3rd party modules.


    Now, I would like to do this:

    for item in bigset:
    try:
    self.__sub1(item)
    self.__sub2(item)
    self.__sub3(item)
    except StandardError:
    # Log error and continue to next item in set.
    log_error_to_file()


    In the error log, I would like to record various local variables that
    existed in subX at the time the Exception was thrown... (even though
    the actuall exception may have been thrown from deep inside some 3rd
    party module that subX called)

    How can I do this?

    Thank you,
    Allen
    allen.fowler, Oct 10, 2007
    #1
    1. Advertising

  2. allen.fowler

    Larry Bates Guest

    allen.fowler wrote:
    > Hi,
    >
    > My code looks like this:
    >
    > for item in bigset:
    > self.__sub1(item)
    > self.__sub2(item)
    > self.__sub3(item)
    >
    > # the subX functions, in turn, use various 3rd party modules.
    >
    >
    > Now, I would like to do this:
    >
    > for item in bigset:
    > try:
    > self.__sub1(item)
    > self.__sub2(item)
    > self.__sub3(item)
    > except StandardError:
    > # Log error and continue to next item in set.
    > log_error_to_file()
    >
    >
    > In the error log, I would like to record various local variables that
    > existed in subX at the time the Exception was thrown... (even though
    > the actuall exception may have been thrown from deep inside some 3rd
    > party module that subX called)
    >
    > How can I do this?
    >
    > Thank you,
    > Allen
    >



    Two possibilieies:

    You will need to determine ALL the exceptions that the 3rd party party modules
    can raise. If they are custom exceptions you will need to import them into your
    application from their modules.

    example:

    say that 3rd party modules raise TransientError, IOError, and ValueError
    exceptions. TransientError is a custom exception from module foo

    from foo import TransientError

    for item in bigset:
    try:
    self.__sub1(item)
    self.__sub2(item)
    self.__sub3(item)
    except (TransientError, IOError, ValueError):
    # Log error and continue to next item in set.
    log_error_to_file()


    2) Hook exception traceback handler

    def myTraceBackHandler(type, value, tb):
    global <variables I want to dump during traceback>
    #
    # This function allows the user to redefine what happens if the program
    # aborts due to an uncaught exception.
    # This provides a way to get a "partial" session log if the program
    # aborts"as well as some information about what caused the program to
    # abort.
    #
    import traceback
    #
    # Get traceback lines
    #
    tblines=traceback.format_exception(type, value, tb)
    #
    # Write some lines to log
    #
    log_error_to_file()
    #
    # Always write the exceptions to screen
    #
    sys.exit('\n'.join(tblines))


    Hope this helps.

    -Larry
    Larry Bates, Oct 10, 2007
    #2
    1. Advertising

  3. allen.fowler

    allen.fowler Guest

    > Two possibilieies:
    >
    > You will need to determine ALL the exceptions that the 3rd party party modules
    > can raise. If they are custom exceptions you will need to import them into your
    > application from their modules.
    >
    > example:
    >
    > say that 3rd party modules raise TransientError, IOError, and ValueError
    > exceptions. TransientError is a custom exception from module foo
    >
    > from foo import TransientError
    >
    > for item in bigset:
    > try:
    > self.__sub1(item)
    > self.__sub2(item)
    > self.__sub3(item)
    > except (TransientError, IOError, ValueError):
    > # Log error and continue to next item in set.
    > log_error_to_file()
    >
    > 2) Hook exception traceback handler
    >
    > def myTraceBackHandler(type, value,tb):
    > global <variables I want to dump during traceback>
    > #
    > # This function allows the user to redefine what happens if the program
    > # aborts due to an uncaught exception.
    > # This provides a way to get a "partial" session log if the program
    > # aborts"as well as some information about what caused the program to
    > # abort.
    > #
    > import traceback
    > #
    > # Get traceback lines
    > #
    > tblines=traceback.format_exception(type, value,tb)
    > #
    > # Write some lines to log
    > #
    > log_error_to_file()
    > #
    > # Always write the exceptions to screen
    > #
    > sys.exit('\n'.join(tblines))
    >
    > Hope this helps.
    >
    > -Larry


    This looks interesting...

    What is myTraceBackHandler? Is it some sort of event handler? Can you
    clarify?

    Thanks,
    Allen
    allen.fowler, Oct 11, 2007
    #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. Christoph Haas
    Replies:
    0
    Views:
    429
    Christoph Haas
    Jun 12, 2006
  2. Christoph Haas
    Replies:
    1
    Views:
    435
    Vinay Sajip
    Jun 14, 2006
  3. Sullivan WxPyQtKinter
    Replies:
    10
    Views:
    628
    Antoon Pardon
    Nov 8, 2007
  4. Tammo Tjarks
    Replies:
    2
    Views:
    272
    Tammo Tjarks
    Sep 13, 2007
  5. king
    Replies:
    2
    Views:
    155
    Tad McClellan
    Jun 27, 2006
Loading...

Share This Page