Write A Python Program To Understand The Use Of Else And Finally Block With Try Block

Your task is to demo a Python program where the Python program is using the   he was like try , except,  else and finally code blocks.

 

Write A Python Program To Understand The Use Of Else And Finally Block With Try Block

 


Try block is used to execute the set of python code that is about to error out or maybe error out when executed.

Except block is used to to handle an uneventful error/  software issues after it occurs.

Else block is used to execute whenever the code present in try block succeeds without any errors

Finally block is like if you have a set of python code it will always be executed, without depending on try/ except if they fail/ pass.





Write A Python Program To Understand The Use Of Else And Finally Block With Try Block

In this example let's  of operation using try except else and finally block.  let's try to open a file in the read mode  using the try block and then if the file is not found, catch an exception.  using the else block read the contents of the data,  then after reading the data close the file.  using the finally block prints the contents of the data.  This will be a perfect example for try, except, else and finally block.

Let's take a variable named data and initialize it to none,  Use try block to open a file named as sample.txt tree in the read mode.  using the except block catch the file not found error,  if the file is not found then print the error message.



data = None
try:
   fd_read = open("sample.txt", "r")
except FileNotFoundError:
   print("The File is Not Found !")

 




If the file is found  then read the contents of the file and store the contents into a data variable using the else block.  once the data is read and print the contents of the file inside the finally block using the print statement.

else:
   data = fd_read.read()
   fd_read.close()
finally:
   print("The Contents of File is : ", data)




Code
===============

data = None
try:
   fd_read = open("sample.txt", "r")
except FileNotFoundError:
   print("The File is Not Found !")
else:
   data = fd_read.read()
   fd_read.close()
finally:
   print("The Contents of File is … \n ", data)





Input : Filename sample.txt is present with contents as below
====================================

Write A Python Program To Understand The Use
Of Else And Finally Block With Try Block



Output
==================

The Contents of File is :  
Write A Python Program To Understand The Use
Of Else And Finally Block With Try Block

Output Screenshot when File is Not Available : Exception is Caught
=======================================================
 
 
except

 
 
 
 
 
Output Screenshot for success scenario
=============================== 


python try block





Conclusion
=============

Execute the above program by providing the sample.txt file in the current working directory,  also try removing the sample.txt file and check if the exception is working fine.


Comment it down below if you have any suggestions on improving the above Python program

Post a Comment

0 Comments