1

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
2

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.