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
user1050619
  • 19,822
  • 85
  • 237
  • 413
  • you imported traceback and then reassigned it in your except clause. You may find what you need in this post: https://stackoverflow.com/questions/3702675/how-to-print-the-full-traceback-without-halting-the-program – whitebeard Dec 03 '17 at 13:40
  • 1
    Possible duplicate of [How to print the full traceback without halting the program?](https://stackoverflow.com/questions/3702675/how-to-print-the-full-traceback-without-halting-the-program) – whitebeard Dec 03 '17 at 13:40

1 Answers1

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

Avishay Cohen
  • 1,978
  • 2
  • 21
  • 34