Python Forum

Full Version: Pythonn 3 getting the __traceback__attribute of an exception
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Python3 documentation states:
A traceback object is normally created automatically when an exception is raised and attached to it as the __traceback__ attribute, which is writable.

What is the trick to get this traceback or what is wrong into my code.
import traceback


class MyException(Exception):

        def __init__(self, mymessage):
                super().__init__

                # do special things
                (print, mymessage)


def essai():

    raise MyException(' myp message')


if __name__ == '__main__':

    essai()
Use
if __name__ == '__main__':
    try:
        essai()
    except Exception as exc:
        t = exc.__traceback__
        print(t)