Files
Python-Schulung/Lehrer/pythonkurs/Mittwoch/utilities.py

30 lines
853 B
Python
Executable File

"""
Miscellaneous utilities
"""
import sys
from traceback import format_tb
# oder print_tb(traceback, file=<handle>)
def hide_exception(func):
try:
return func()
except Exception as e:
name = type(e).__name__
text = str(e)
traceback = ''.join(format_tb(e.__traceback__))
if '-V' in sys.argv or '--with-exception' in sys.argv:
# Fuer Faule, einfach existierende Exception weiterleiten
# raise
print("Traceback:\n", traceback, sep='', file=sys.stderr)
print("{name}: {text}".format(name=name, text=text), file=sys.stderr)
else:
print("Interner Fehler,", e)
# TODO: Exception-Informationen in Logdatei schreiben
if __name__ == '__main__':
print("from utilities import hide_exception")
print("hide_exception(main)")