Can we copy stack trace of Exception in a string?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • indiarocks
    New Member
    • Feb 2007
    • 20

    Can we copy stack trace of Exception in a string?

    Hi,

    Can a stack trace be generated for the exception that the program catches using except or except IOError etc

    eg:
    Code:
      try:  
           raise Exception
      except Exception,e:
          print the stack trace that caused this exception
    Thanks
    Last edited by bartonc; Feb 22 '07, 08:22 PM. Reason: added [code][/code] tags
  • indiarocks
    New Member
    • Feb 2007
    • 20

    #2
    Was looking around online and found this

    import sys, traceback
    Code:
    def func1()
        try:
            do something that raises exception
        except:
            traceback.print_exc(file=sys.stdout)
    That would print the stack trace.
    Last edited by bartonc; Feb 22 '07, 08:20 PM. Reason: added [code][/code] tags

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by indiarocks
      Was looking around online and found this

      import sys, traceback
      Code:
      def func1()
          try:
              do something that raises exception
          except:
              traceback.print_exc(file=sys.stdout)
      That would print the stack trace.
      That looks like just the thing, but I'll check it out for you.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by indiarocks
        Was looking around online and found this

        import sys, traceback
        Code:
        def func1()
            try:
                do something that raises exception
            except:
                traceback.print_exc(file=sys.stdout)
        That would print the stack trace.
        1) You'll learn how to use code tags by reading the "POSTING GUIDELINES" or "REPLY GUIDELINES" panel on the right hand side of the page while you are posting.

        2) In section 3.12 of the Python Library Reference (in the python help file) it says:
        format_exc( [limit])
        This is like print_exc(limit ) but returns a string instead of printing to a file. New in version 2.4.

        Comment

        • ghostdog74
          Recognized Expert Contributor
          • Apr 2006
          • 511

          #5
          in your code:

          Code:
          ....
          except Exception,e:
          you can just print the value of 'e' and see what it says

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by ghostdog74
            in your code:

            Code:
            ....
            except Exception,e:
            you can just print the value of 'e' and see what it says
            You are just full of cool tricks. Thanks GD.

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Following are a couple of functions we use to format the error messages:
              Code:
              import sys, traceback
              
              def formatExceptionInfo(level = 6):
                  error_type, error_value, trbk = sys.exc_info()
                  tb_list = traceback.format_tb(trbk, level)    
                  s = "Error: %s \nDescription: %s \nTraceback:" % (error_type.__name__, error_value)
                  for i in tb_list:
                      s += "\n" + i
                  return s
              
              def formatExceptionInfo1():
                  return '%s\n%s' % ('The script was cancelled by the user', ''.join(traceback.format_tb(sys.exc_info()[2])))
              Phil Adams contributed to the first function.

              Comment

              • ghostdog74
                Recognized Expert Contributor
                • Apr 2006
                • 511

                #8
                Originally posted by bartonc
                You are just full of cool tricks. Thanks GD.
                no problem. Well actually its documentedhere
                Also, OP has it included in his code, so i was wondering why he did not use it.

                Comment

                • bartonc
                  Recognized Expert Expert
                  • Sep 2006
                  • 6478

                  #9
                  Originally posted by ghostdog74
                  no problem. Well actually its documentedhere
                  Also, OP has it included in his code, so i was wondering why he did not use it.
                  Yeah. I saw that and assumed that the OP needed the value in a string (due to the title of the post).

                  Comment

                  Working...