Python Exception Handling

Python Exception Handling, Errors and Exceptions, Raising Exceptions, Exception Chaining, User-defined Exceptions, and Defining Clean-up Actions.

Types of Errors in Computer Programs

Human Errors

result = x*y

Desired output: 100
Getting: 90

print(“Hello Python”)
x=100
y=900

if (x>y):
pass

Note: ‘pass’ keyword ignores the incomplete code

No Error

x=100
y=900

if (x>y):
print (“X is Big Number”)

1. Syntax Errors / Compile-time errors

Ex:
x=100
y=900

If (x>y):
print (“X is Big Number”)

2. Run-Time Errors / Run-time errors

Ex:

x=10
y=0
print(x/y)

Python Exception Handling

What is Exception?

An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program’s instructions.

When a Python program/script raises an exception, it must either handle the exception immediately otherwise it terminates and quits.

Python provides two very important features to handle any unexpected error in your Python programs and to add debugging capabilities in them −

1. Exception Handling
2. Assertions

Common Exceptions in Python

A list of common exceptions that can be thrown from a standard Python program is given below.

1. ZeroDivisionError: Occurs when a number is divided by zero.
2. NameError: It occurs when a name is not found. It may be local or global.
3. IndentationError: If incorrect indentation is given.
4. IOError: It occurs when Input-Output operation fails.
5. EOFError: It occurs when the end of the file is reached, and yet operations are being performed.


Example 1: Python Program (ZeroDivisionError)

x = int(input(“Enter x: “))
y = int(input(“Enter y: “))
z = x/y
print(z)

print(“Hello Python Programmers”)

In the above program, we have two variables x and y, which take the input from the user and perform the division of these values. What if the user entered zero as the denominator? It will interrupt the program execution and through a ZeroDivision exception.

A Python Program with Exception handling Code

Syntax:

try:
statement/s
.
.
except:
statement/s
.
.
Other statement/s
.
.

————————-

try:
x = int(input(“Enter Num1: “))
y = int(input(“Enter Num2: “))
print(x/y)
except:
print(“Can’t divide with zero”)

print(“Hello Python”)
print (“I am a Python Programmer”)

————————-

Or

try:
x = int(input(“Enter Num1: “))
y = int(input(“Enter Num2: “))
z=x/y
except:
print(“Can’t divide with zero”)
else:
print(z)

print(“Hello Python”)
print (“I am a Python Programmer”)


Example 2: IndexError

mylist = [10, 20, 30, 40, 4.567, “India”]
print(mylist)

i = int(input(“Enter an Index: “))
print(mylist[i])

print(“Hello Python”)
print (“I am a Python Programmer”)

————————-

A Python Program with Exception handling Code

mylist = [10, 20, 30, 40, 4.567, “India”]
print(mylist)

try:
i = int(input(“Enter an Index: “))
print(mylist[i])
except:
print(“Object index out of range”)

print(“Hello Python”)
print (“I am a Python Programmer”)


Example 3: NameError

mycountry=”India”

print(mycounry)

print(“Hello Python”)
print (“I am a Python Programmer”)

————————-

A Python Program with Exception handling Code

mycountry=”India”

try:
print(mycoutry)
except:
print(“NameError”)

print(“Hello Python”)
print (“I am a Python Programmer”)


Python Tutorial
Python Video
Follow me on social media: