Generate a code to print the stack trace in Python when an exception occurs.
import traceback

try:
    # some code that might generate exceptions
    pass
except Exception as e:
    print(traceback.format_exc())