Last active
December 1, 2024 06:50
-
-
Save DavidBuchanan314/7cdd3c34fedd0c5175d301a38e6922a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
(try_except := (lambda fn, exception_callback: | |
type("mycontext", (__import__("contextlib").ContextDecorator,), { | |
"__enter__": lambda self: self, | |
"__exit__": lambda self, a, b, c: (a and exception_callback(a, b, c)) or True | |
})()(fn)() | |
)), | |
(throw := lambda e: | |
(_ for _ in ()).throw(e) | |
), | |
print(try_except( | |
lambda: int("123"), | |
lambda a, b, c: print("caught", a, b, c) | |
)), | |
print(try_except( | |
lambda: int("abc"), | |
lambda a, b, c: print("caught", a, b, c) | |
)), | |
print(try_except( | |
lambda: throw(Exception("myexception")), | |
lambda a, b, c: print("caught", a, b, c) | |
)), | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment