How to Handling Exception in Python 3.8?

Before starting the tutorial it must be to understand what actually exception in python?

What is Exception in Python 3.8?

An exception is an event which accrue during the execution or run the programming in any programming languages, that disturb the normal flow of the program or application.

If you don’t read my previous article please read this  have teach you how to create abstraction classes in python. Today we will learn how to handling exception in python and what are the major types of exception in python.

Here are we write the some random exception in python

1. ZeroDivisionError

This exception will occur when you try to dividing a number with zero. Then this type of exception will occur.

>>10/0

Traceback (most recent call last):

  File “<input>”, line 1, in <module>

ZeroDivisionError: division by zero

2. TypoError

This exception will occur when you try solving number with some string. Then this type of exception will occur. Example of this below.

>> 20 / “abc”

 

Traceback (most recent call last):

  File “<input>”, line 1, in <module>

TypeError: unsupported operand type(s) for /: ‘int’ and ‘str’

3. AttributeError

This exception will occur when you try to access a variable value without any attribute which is not defined in python library. Then this type of exception will occur. Example of this below.

>>x =[2,4,7]

>>x.jiu

Output:

Traceback (most recent call last):

Traceback (most recent call last):

  File “<input>”, line 1, in <module>

AttributeError: ‘list’ object has no attribute ‘jiu’

handling exception in python 3

In the above example jiu has no attribute so it give me AttribueError.

Try to access variable with right attribute

print(x)

Output is:

[2, 4, 7]

Example 01. Handling the ZeroDevisionValue

Suppose you make a little program which you want user solve two numbers by input the value. So you need to write some code in python file which user can access

#handling exception in python 3
#python exception types
result= None
a = float (input ("Enter first number :"))
b = float (input ("Enter second number :"))

result = a/b

print (‘Rsult :’,result)

Run the code and enter the first and 2nd number.

Enter first number :10

Enter second number :0

it will give you an error

Output is:

Traceback (most recent call last):

  File "G:/Project/sharafat/abstract_classes/zerovalue.py", line 4, in <module>

    result = a/b
ZeroDivisionError: float division by zero

Above error occur because user input the second number is zero. And zero is not can’t be divided with a number. So let’s write some code to handle this type of exception.

#learn python 3
#handling exception in python 3
result= None
a = float (input ("Enter first number :"))
b = float (input ("Enter second number :"))
try:
    result = a/b
except:
    print("float can’t divided with zero")
print ('Rsult :',result)

run the code again and you will see the following out put. Which have no error.

Enter first number :20

Enter second number :0

float can not divided with zero

Rsult : None

 

Process finished with exit code 0

Except statement will print when an event or error occur while execution the program. If the use input the write number and you will see in the output section this statement will not be print. And output will be the correct.

Enter first number :20

Enter second number :5

Rsult : 4.0

 

Process finished with exit code 0

If you want to show to more detail or type of exception in python you can modify the exception line

#handling exception in python 3


except
ZeroDivisionError as e:
    print("Error",type(e))

once again run the code and input zero value in the  2nd number . you will see the detail of error.

Enter first number :23

Enter second number :0

Error <class 'ZeroDivisionError'>

Rsult : None

 

Process finished with exit code 0

This Post Has 2 Comments

  1. Bedava Transformice Hesap (Pro) 2021 | Hesap şifresi. Peki
    Transformice nedir? Transformice, son zamanlarda
    popüler olan ve geçtiğimiz yıl birçok oyuncu tarafından oynanan çevrimiçi bir tarayıcı oyunudur.
    Adından da anlaşılacağı gibi oyundaki ana karakterimiz bir fare ve bu fareyi kontrol etmek görevimiz.
    Farklı ve zorlu bölümleri olan oyunda amacımız tüm
    engelleri aşmak ve peynire ulaşarak bölümü tamamlamaktır.
    Yani oyunun eğlenceli kısmı sadece lav gibi faktörler değil, aynı zamanda
    diğer farelerin de düşmanınız olması.
    Yani odadaki tüm farelerle rekabet içindesiniz.Belirtilen süre içerisinde peynire ilk ulaşan kişi puan alır.

    Böylece puanla birinci olan oyuncu diğer el
    tur lideri olarak oyuna devam eder. Yani tur liderlerine şaman faresi de deniyor.

    Bedava Transformice Hesap

Leave a Reply