import sys
import traceback
import cgitb

def handleException(excType, excValue, trace):
    print('error')
cgitb.Hook(format='text')(excType, excValue, trace)
sys.excepthook = handleException
h = 1
k = 0
print(h / k)
Python catch any exception, and print or log traceback with variable values