Created
May 19, 2025 15:33
-
-
Save jsbueno/a22326c54059ac473a62bb4ffeb2fd18 to your computer and use it in GitHub Desktop.
Example for getting an exception callback even when the exception is handled in inner code:
This file contains hidden or 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
# This makes use of sys.monitoring, described in PEP 669 and implemented in Python 3.12 | |
import time | |
import sys | |
def aha(*args): | |
print(args) | |
def worker(): | |
while True: | |
time.sleep(1) | |
try: | |
# this error sould not ordinarily be visible in any | |
# outer scope: | |
1/0 | |
except ZeroDivisionError: | |
pass | |
TOOL_ID = 3 | |
def start(): | |
worker() | |
def main(): | |
watched = sys.monitoring.events.RAISE | |
try: | |
x = sys.monitoring.use_tool_id(TOOL_ID, "test") | |
sys.monitoring.register_callback(TOOL_ID, watched, aha) | |
sys.monitoring.set_events(TOOL_ID, watched) | |
start() | |
finally: | |
sys.monitoring.free_tool_id(TOOL_ID) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment