Skip to content

Instantly share code, notes, and snippets.

@DavidBuchanan314
Last active December 1, 2024 06:50
Show Gist options
  • Save DavidBuchanan314/7cdd3c34fedd0c5175d301a38e6922a1 to your computer and use it in GitHub Desktop.
Save DavidBuchanan314/7cdd3c34fedd0c5175d301a38e6922a1 to your computer and use it in GitHub Desktop.
[
(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