2

I recently came across cgitb. It's awesome, especially for the kind of thing I was trying to solve.

However, I have a simple requirement :

Is it possible to write out the exception trace on to a file and still continue post that as well. I know how to write out the exception to some file, but I am looking for the latter part.

Here's what I have tried:

import cgitb
cgitb.enable()

'''
try:
   print(str(10/0))
except Exception as e:
  print "Hello"    
print "Tesla"
'''

So with the try-except block uncommented and without cgitb, I get Tesla printed out as well after the stack trace. But with cgitb enabled and the try-except block commented out, although I get a better stack trace, Tesla does not get printed out. Is there someway that we can still get Tesla printed out while using cgitb

qre0ct
  • 5,680
  • 10
  • 50
  • 86
  • If you catch the exception the `cgitab` won't trace it. `cgitab` is just a detailed traceback and it doesn't affect your code. – Masoud Rahimi Apr 23 '19 at 11:42
  • This is not an answer (I do not have enough reputation to comment), but please note that cgitb might be deprecated in Python3.8 and removed in Python 3.10: https://www.python.org/dev/peps/pep-0594/#cgitb – Lan Quil Jun 03 '19 at 06:29

1 Answers1

0

As the documentation says, you can call cgitb.handler yourself when you catch an exception. Execution then continues, albeit from the handler rather than from the raise or so; Python does not have resumable exceptions.

Davis Herring
  • 36,443
  • 4
  • 48
  • 76
  • ok so on your advice, i tried doing it this way : ```except: cgitb.handler()``` and with that I get the `Tesla` string printed along with the stack trace. However, the stack trace is now being printed in html despite having cgitb.enable(format=text) in place. Any pointers? – qre0ct Apr 23 '19 at 13:10
  • @qre0ct: Python 3 has [`cgitb.text`](https://docs.python.org/3.7/library/cgitb.html?highlight=cgitb.text#cgitb.text), but that’s all I see. – Davis Herring Apr 23 '19 at 13:49