python how to safely handle an exception inside a context manager
问题 I think I've read that exceptions inside a with do not allow __exit__ to be call correctly. If I am wrong on this note, pardon my ignorance. So I have some pseudo code here, my goal is to use a lock context that upon __enter__ logs a start datetime and returns a lock id, and upon __exit__ records an end datetime and releases the lock: def main(): raise Exception with cron.lock() as lockid: print('Got lock: %i' % lockid) main() How can I still raise errors in addition to existing the context