class Context: def __enter__(self): print("Entering") def __exit__(self, *args): print("Exiting with",[type(x) for x in args]) raise TypeError() def withException(): print("Before") with Context() as c: print("Raising") raise ValueError() print("Don't print me") print("After") try: withException() except Exception as e: print(repr(e))