Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
try, except, finally ?
#3
Here is an example
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.
Reply


Messages In This Thread
try, except, finally ? - by microphone_head - Apr-28-2019, 08:45 PM
RE: try, except, finally ? - by ichabod801 - Apr-28-2019, 09:07 PM
RE: try, except, finally ? - by Yoriz - Apr-28-2019, 09:18 PM
RE: try, except, finally ? - by microphone_head - Apr-28-2019, 09:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with try, except, else finally Pytho13 14 5,282 Mar-23-2021, 05:35 PM
Last Post: snippsat
  Am I a retard - else and finally blocks in a try statement RubenF85 6 2,732 Jan-12-2021, 05:56 PM
Last Post: bowlofred
  finally clause Skaperen 6 4,009 Jun-02-2019, 09:02 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020