Python Forum
Am I a retard - else and finally blocks in a try statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Am I a retard - else and finally blocks in a try statement
#6
Is having code in the "else" block any better than code in the "try" block? Is it because doing so places a harder focus on what code is expected to raise an exception?
try:
    dangerous code
    innocuous code
except:
    handler code

#vs

try:
    dangerous code
except:
    handler code
else:
    innocuous code
The upper example has the benefit of keeping the code together instead of splitting into little pieces. The bottom example has the benefit of pointing out what code is handled by the exception.

Does finally predate context managers? I can see using finally to close a file or do some other cleanup that is not done by other tools. I can see that it eliminates the need for duplicate code in the except and else blocks, but did it have a more important role?
Reply


Messages In This Thread
RE: Am I a retard - else and finally blocks in a try statement - by deanhystad - Jan-12-2021, 05:25 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  RSA Cipher with blocks Paragoon2 0 626 Nov-26-2023, 04:35 PM
Last Post: Paragoon2
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 2,178 Dec-12-2022, 08:22 PM
Last Post: jh67
  Help with try, except, else finally Pytho13 14 5,454 Mar-23-2021, 05:35 PM
Last Post: snippsat
  How to tabulate correctly repeated blocks? Xiesxes 4 3,133 Mar-21-2020, 04:57 PM
Last Post: Xiesxes
  try/except blocks newbieAuggie2019 11 5,148 Oct-05-2019, 05:55 PM
Last Post: newbieAuggie2019
  Understanding program blocks newbieAuggie2019 2 2,129 Oct-02-2019, 06:22 PM
Last Post: newbieAuggie2019
  finally clause Skaperen 6 4,083 Jun-02-2019, 09:02 PM
Last Post: snippsat
  try, except, finally ? microphone_head 3 2,994 Apr-28-2019, 09:36 PM
Last Post: microphone_head
  The Empty List Blocks Recursion leoahum 6 5,588 Mar-05-2019, 06:55 PM
Last Post: leoahum

Forum Jump:

User Panel Messages

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