Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
finally clause
#7
(Jun-02-2019, 08:34 PM)Skaperen Wrote: so if the exception might happen, finally: assures that you can close/teardown while not actually handling the exception
Yes,and we use it all time even if not always see it.
with open() has been then preferred way do deal with files since it came out in Python 2.5.
with open('some_file', 'w') as f:
    f.write('Hello!')
The underlying code is this.
f = open('some_file', 'w')
try:
    f.write('Hello!')
finally:
    f.close()
Now is there also __enter__ and __exit__ special methods to make with.
But the important part is that the file is always closed because of no except and finally(deal with all cases for closing the file).
Reply


Messages In This Thread
finally clause - by Skaperen - Jun-02-2019, 04:37 AM
RE: finally clause - by perfringo - Jun-02-2019, 05:10 AM
RE: finally clause - by heiner55 - Jun-02-2019, 06:39 AM
RE: finally clause - by Yoriz - Jun-02-2019, 07:01 AM
RE: finally clause - by Skaperen - Jun-02-2019, 08:34 PM
RE: finally clause - by perfringo - Jun-02-2019, 08:45 PM
RE: finally clause - by snippsat - Jun-02-2019, 09:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use the LIKE clause in Python Columbo 9 1,786 Oct-09-2022, 10:22 PM
Last Post: Larz60+
  SQL Query is not executing WHERE clause hammer 7 2,495 Nov-15-2021, 01:44 PM
Last Post: hammer
  How does this if clause work? Pedroski55 3 2,361 Jun-10-2021, 06:31 AM
Last Post: Gribouillis
  Help with try, except, else finally Pytho13 14 5,293 Mar-23-2021, 05:35 PM
Last Post: snippsat
  Am I a retard - else and finally blocks in a try statement RubenF85 6 2,745 Jan-12-2021, 05:56 PM
Last Post: bowlofred
  can i raise an exception in a try clause? Skaperen 14 5,971 Dec-19-2019, 12:29 AM
Last Post: Skaperen
  pass captured value from input() to where clause metro17 5 3,397 Sep-09-2019, 05:24 AM
Last Post: metro17
  try, except, finally ? microphone_head 3 2,942 Apr-28-2019, 09:36 PM
Last Post: microphone_head
  if clause fails DeadCthulhuWaitsDreaming 10 4,935 Apr-07-2019, 09:19 PM
Last Post: DeadCthulhuWaitsDreaming
  how to code in Python "where" clause of SAS FelixS 2 2,888 Mar-26-2019, 04:59 PM
Last Post: FelixS

Forum Jump:

User Panel Messages

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