traceback.print_exc() supposed to stop exception propagation.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sami

    traceback.print_exc() supposed to stop exception propagation.

    Hello,

    In the Python book that I am using to learn the language it says that
    the traceback.print _exc() can be used to stop exception propagation and
    make the program keep running.

    Here is a simple piece of code that I typed in to test this fact:
    ---------------------------------------------------------------------------
    import sys

    def Myexcepthook(et ype, value, tb):
    print "in Myexcepthook\n"
    import traceback
    lines=traceback .format_excepti on(etype, value, tb)
    print "\n".join(lines )
    traceback.print _exc()


    sys.excepthook = Myexcepthook

    x = 1/0

    x = 78

    print x
    --------------------------------------------------------------------------
    The Output:
    --------------------------------------------------------------------------
    in Myexcepthook

    Traceback (most recent call last):

    File
    "E:\Home\Progra mming\Python\Tr yProjects\Excep tHandling1\Exce pt2.py", lin
    15, in <module>
    x = 1/0

    ZeroDivisionErr or: integer division or modulo by zero

    None
    --------------------------------------------------------------------------

    I never see the value 78.

    What am I doing wrong?

    Thanks,
    Sami
  • Diez B. Roggisch

    #2
    Re: traceback.print _exc() supposed to stop exception propagation.

    Sami schrieb:
    Hello,
    >
    In the Python book that I am using to learn the language it says that
    the traceback.print _exc() can be used to stop exception propagation and
    make the program keep running.
    >
    Here is a simple piece of code that I typed in to test this fact:
    ---------------------------------------------------------------------------
    import sys
    >
    def Myexcepthook(et ype, value, tb):
    print "in Myexcepthook\n"
    import traceback
    lines=traceback .format_excepti on(etype, value, tb)
    print "\n".join(lines )
    traceback.print _exc()
    >
    >
    sys.excepthook = Myexcepthook
    >
    x = 1/0
    >
    x = 78
    >
    print x
    --------------------------------------------------------------------------
    The Output:
    --------------------------------------------------------------------------
    in Myexcepthook
    >
    Traceback (most recent call last):
    >
    File
    "E:\Home\Progra mming\Python\Tr yProjects\Excep tHandling1\Exce pt2.py", lin
    15, in <module>
    x = 1/0
    >
    ZeroDivisionErr or: integer division or modulo by zero
    >
    None
    --------------------------------------------------------------------------
    >
    I never see the value 78.
    >
    What am I doing wrong?
    Trusting a wrong source. Or misinterpreting it.

    Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
    [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
    Type "help", "copyright" , "credits" or "license" for more information.
    imWelcome to rlcompleter2 0.96
    for nice experiences hit <tabmultiple times
    >>import traceback
    >>help(tracebac k.print_exc)
    Help on function print_exc in module traceback:

    print_exc(limit =None, file=None)
    Shorthand for 'print_exceptio n(sys.exc_type, sys.exc_value,
    sys.exc_traceba ck, limit, file)'.
    (In fact, it uses sys.exc_info() to retrieve the same information
    in a thread-safe way.)
    >>>
    Nothing in there says that this would prevent the exception from being
    propagated.

    Diez

    Comment

    • Terry Reedy

      #3
      Re: traceback.print _exc() supposed to stop exception propagation.


      "Sami" <sami.islam@NOS PAMbtinternet.c omwrote in message
      news:69-dnW_JftMHvGTanZ 2dneKdnZydnZ2d@ bt.com...
      | Hello,
      |
      | In the Python book that I am using to learn the language it says that
      | the traceback.print _exc() can be used to stop exception propagation and
      | make the program keep running.

      It is possible that the unspecified book describes an unspecified Python
      version that is not the same as the unspecified version that you tested
      with ;-).

      Help respondants by providing version info. Sometimes even the system/OS
      info is helpful, though probably not relevant here.

      tjr



      Comment

      Working...