Day #37 - Finally Keyword

Day #37 - Finally Keyword

ยท

3 min read

Introduction

Welcome to my 37th blog post. There are certain statements that we compulsorily want to execute, even if the program disruption happens due to some error or breakdown then we make use of the 'finally' keyword. 'finally' keyword is used in exception handling. In today's blog, I am going to share my learnings from the day 37th of my python coding journey. Now Let's dive into more details.

So let's get started......

'finally' keyword

When we handle exceptions in python, using the 'try and except' block that we learnt in the last blog, we can include a 'finally' block at the end. The code written in the ''finally'' block is always executed. So it is generally used for doing the tasks that provide the conclusion or an end to any program like closing file resources or closing database connections or ending the program execution with a delightful message.

Syntax

try:
   #statements which could generate
   #exception
except:
   #solution of generated exception
finally:
    #block of code which is going to
    #execute in any situation

In the above code, the finally block of code is executed in any situation come what may. 'finally' block finds its main application while writing functions in python.

Example -

try:
    num = int(input("Enter an integer: "))
except ValueError:
    print("Number entered is not an integer.")
else:
    print("Integer Accepted.")
finally:
    print("This block is always executed.")

Output 1 -

Enter an integer: 25
Integer Accepted.
This block is always executed.

Output 2 -

Enter an integer: 3.142
Number entered is not an integer.
This block is always executed.

'Finally' Clause using function

def func1():
  try:
    l = [1, 5, 6, 7]
    i = int(input("Enter the index: "))
    print(l[i])
    return 1
  except:
    print("Some error occurred")
    return 0

  finally:
    print("I am always executed")

  print("I am always executed, but i am without finally")
#If above statement if not written in function then it is executed everytime. But when written in function then it is not executed. But if we use 'finally' keyword in function then it is executed just like previous statement.

x = func1()
print(x)
Enter the index: 5
Some error occurred
I am always executed
0

Resources Used

You can watch the video of Day#37 by clicking on the below link ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

Conclusion

Thanks, guys for going through this blog post. In this blog post, we learned about how we can use the 'finally' keyword to compulsorily execute a set of statements. Finally keyword finds its most important application while writing the functions.

Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of python coding. This is it for Day #37

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.


ย