Explain try, except and finally statements in Python.

PythonServer Side ProgrammingProgramming

In exception handling in Python, we use the try and except statements to catch and handle exceptions. The code within the try clause is executed statement by statement.

If an exception occurs, the rest of the try block is skipped and the except clause is executed.

Example

try:
'apple' + 6
except Exception:
print "Cannot concatenate 'str' and 'int' objects"

Output

Cannot concatenate 'str' and 'int' objects

We avoid the traceback error message elegantly with a simple message like above by using try except statements for exception handling.

In addition to using an except block after the try block, we can also use the finally block. The finally clause is optional. It is intended to define clean-up actions that must be executed under all circumstances

A finally clause is always executed before leaving the try statement, whether an exception has occurred or not.

Actions, like closing a file, GUI or disconnecting from the network, are performed in the finally clause to guarantee execution.

Here is an example of file operations to illustrate finally statement.

Example

try:
f = open("foo.txt",encoding = 'utf-8')
# perform file operations
finally:
f.close()

This type of statement makes sure that the file is closed whether an exception occurs or not.

raja
Published on 07-Dec-2017 11:11:04
Advertisements