user1050619
user1050619

Reputation: 20856

Get traceback in python exception

I'm trying to pass exception to my main function with the traceback but its not working as expected.

import sys
import traceback

def test_function():
    return 0/0

def task1():
    try:
        a = 1
        test_function()
    except Exception as e:      
        print e
        traceback = sys.exc_info()[2]
        raise Exception(), 'Error message', traceback       

def main():
    try:
        task1()
    except Exception, e:
        print e

print 'start'
main()  
print 'end'

Here is my result:-

start
integer division or modulo by zero
instance exception may not have a separate value
end

Upvotes: 1

Views: 789

Answers (1)

Avishay Cohen
Avishay Cohen

Reputation: 2208

traceback is the name of the module, try using it's methods, like traceback.print_stack() which will print out the stacktrace like how you see it when you don't catch the error.

see more in here: traceback doc

you can use traceback.extract_stack() to get a list of tuples of the stack

Upvotes: 2

Related Questions