Apr-28-2019, 09:18 PM
Here is an example
The file close in the finally still happens, if there was no finally the one after would not close the file.
try: print('file open') raise PermissionError except FileNotFoundError as exception: print(exception) finally: print('file closed in finally') print('file closed outside try/except/finally')
Error:file open
file closed in finally
Traceback (most recent call last):
File "C:\Users\Dave\Documents\Eclipse Workspace\Test\forum\forum_post.py", line 4, in <module>
raise PermissionError
PermissionError
The code is expecting to get a FileNotFoundError
but there is actually a unexpected PermissionError
The file close in the finally still happens, if there was no finally the one after would not close the file.