Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exception Handling
#1
Hi,

There are two constructs that Python offers on exceptions. One is try-except-finally and the other is try-finally.

I am trying to understand in which situations do we use try-finally? Since using try-finally we cannot catch any exceptions then why does Python provide this construct, when we can use the try-except-finally.

Thanks.
Reply
#2
They are not exclusive. Look at this example from PEP 341:

try:
    block-1 ...
except Exception1:
    handler-1 ...
except Exception2:
    handler-2 ...
else:
    else-block
finally:
    final-block
First, block-1 executes. If block-1 raises Exception1, then handler-1 is executed. If block-1 raises Exception2, then handler-2 is executed. If block-1 raises no exceptions, then the else-block is executed. Finally, no matter what happened in block-1, final-block is executed. Note that final-block is executed even if block-1 raises an exception besides Exception1 or Exception2, which would skip handler-1, handler-2, and else-block.

The except clause is for handling what happens with a given error. The finally block is for something you do no matter what happens. The except block is typically for redoing the processing in a different manner based on the exception raised. The finally block is typically for cleaning up resources. Say you opened a file in block-1. You could use the final-block to close the file. Of course, we have context managers and the with statement for handling files now. But this common use for cleaning up files is why the with statement was created.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
you can have a try: ... except ValueError ... finnaly ...
sequence for example.
if there is a ValueError raised during the try clause, the except clause will be executed, but not if there as an exception that is not a value error. In both cases, finally will be executed.
Reply
#4
try/finally allows you to ignore exceptions. Maybe someone smarter than me can think of a compelling use case for such a construct Think , but my guess will be that interpreter checks that either except or finally block(s) follow the try block.

Being given the rope does not mean that you should hang yourself, though
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,217 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  TicTacToe Game Add Exception Handling and Warning Function ShaikhShaikh 5 2,376 Nov-03-2021, 05:02 PM
Last Post: deanhystad
  Exception handling in regex using python ShruthiLS 1 2,330 May-04-2020, 08:12 AM
Last Post: anbu23
  Exception handling Calli 2 2,409 Apr-20-2020, 06:13 PM
Last Post: Calli
  Handling exception from a module dchi2 11 5,500 Nov-25-2019, 08:47 AM
Last Post: dchi2
  problem using custom exception handling in python srm 3 3,000 Jul-03-2019, 09:10 PM
Last Post: ichabod801
  an easy way to disable exception handling Skaperen 6 5,307 Jun-02-2019, 10:38 PM
Last Post: Gribouillis
  exception handling KyawMyo 3 2,782 May-07-2019, 07:53 AM
Last Post: buran
  Database operation exception handling LostInCode 1 2,449 Jan-03-2019, 07:50 PM
Last Post: jeanMichelBain
  During handling of the above exception, another exception occurred Skaperen 7 26,728 Dec-21-2018, 10:58 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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