Introduction
Welcome to my 36th blog post. Human error is common while writing the code of any program. Mistakes can also be made at the server’s end as well, for example, we asked for some records from the server and the records could not be fetched due to some server error. This could lead to program termination unexpectedly. We do not want this kind of program termination and hence we do error handling in python. To handle python errors we make use of 'Try except' for code. Now Let's dive into more details.
So let's get started......
Exception Handling
It is the process to respond by raising an error to unwanted or unexpected events when a computer program runs. Exception handling helps in dealing with unexpected events like program or system crashes. If Exception handling is not done then it could disrupt the normal flow of the program.
Exceptions in Python
Many built-in python exceptions can be raised when our program encounters an error (something in the program goes wrong).
When these exceptions occur, the Python interpreter stops the current process and passes it to the calling process until it is handled. If exceptions are not handled, then the program will crash.
Python try...except block
try….. except blocks are used in python to handle errors and exceptions. The code in try block runs when there is no error. If the try block catches the error, then the except block is executed.
Syntax
try:
#statements which could generate
#exception
except:
#Soloution of generated exception
Example -
a = input("Enter the number: ") #here we will raise error by using string
print(f"Multiplication table of {a} is: ")
try:
for i in range(1, 11):
print(f"{int(a)} X {i} = {int(a)*i}")
# except Exception as e:
# print(e)
except:
print("Invalid Input!")
print("Some imp lines of code")
print("End of program")
Enter the number: cj
Multiplication table of cj is:
Invalid Input! #error raised
Some imp lines of code
End of program
More Example -
try:
num = int(input("Enter an integer: "))
a = [6, 3]
print(a[num])
except ValueError:
print("Number entered is not an integer.")
except IndexError:
print("Index Error")
Enter an integer: 6
Index Error
Resources Used
You can watch the video of Day#36 by clicking on the below link 👇👇👇👇👇
Conclusion
Thanks, guys for going through this blog post. Through this Blog post, we learned about how we can raise errors and handle them using try and except block. This will help us to maintain the program flow without any disruption.
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 #36
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.