Exception Handling in Python: An Introduction(GUIDE)

Hey, Coders, Today, I’m going to explain Exception Handling in Python. In this article, I’m going to tell you that How you can Raise an Exception in Python and handle them to prevent your program from termination.

What Is Exception ?

During the program execution, Sometimes your programs terminated due to some unusual error occur, Which also known as Exceptions. Now, It’s the responsibility of the program to handle these exception and prevent the program from termination.

To prevent our program from termination, We have to handle these exceptions using try and except block. But before that let me show you a simple example of program that got terminated due to an error.

In this given program, I declared an age variable to get an integer input. But instead of entering integer value, I’ll enter string value. Therefore, our programs will get terminated by ValueError exception like this:

PROGRAM 1:
age = int(input("Enter Your Age: "))
Enter Your Age: ds

Traceback (most recent call last):
  File "C:/Users/Prashant Karn/Desktop/HK2LITE PROGRAMS/Pythons/Exceptions/Exceptions.py", line 1, in <module>
    age = int(input("Enter Your Age: "))
ValueError: invalid literal for int() with base 10: 'ds'

Process finished with exit code 1

In this above code, You can see that our program got terminated and displayed ValueError.

Exception Handling In Python

In above code, You just saw that How our program terminated due to an ValueError exception. Therefore, To prevent our program from these kind of termination, we have to handle these exception. It’s the responsibility of programmers to handle these exception and display friendly message to users.

To Handling Exception in Python, We need to use “try” and “except” clause. These clause will allow programmers to display friendly message. It will also prevent your program from termination.

PROGRAM 2:

Here, I’m going to Handle the exception of Program 1. First, Let me show you my program that I’ve written to handle error:

try:
    age = int(input("Enter Your Age: "))
except ValueError:
    print("Enter Valid Age")
Enter Your Age: four
Enter Valid Age

In this above code, You can see that I’ve used try block and insert my program inside the try block. Then I’ve use except clause to display a friendly message.

In try block, You have to insert your statement or program. Then In except block, You have to define an error with except clause that you want to handle. Just like here, we want to handle ValueError exception. Therefore, We will use ValueError with except clause. Inside this clause, just print your message.

Handle Different Exception At One Time:

In Python, We have lot of different exception that occurs due to different types of errors. You can check all Python exception from their Official Website: Click Here

Here, You can also handle different type of exception at one time, which belongs to same message. In above code, We handled an exception to prevent wrong data type entries error. In that same code, we will define an additional exception called “ZeroDivisionError”. ZeroDivisionError Exception occur when someone try to divide any integer with 0.

    try:
        age = int(input("Enter Your Age: "))
        num1 = 10/age
    except (ValueError, ZeroDivisionError):
        print("Enter Valid Age")

In this above, You can see that we handled our “ZeroDivisionError” along with “ValueError” exception inside except clause.

You May Also Like: Classes Introduction in Python Programming

That’s it for today. It’s just a basic overview introduction of Exception and Handling Exception in Python. If you wanna get updated with such kind of programming related post then please don’t forget to subscribe our blog via email.

Thank’s To Read…

You may also like

Leave a Comment

Your email address will not be published. Required fields are marked *