Exception Handling in Python
By: Akhil Anand
Tags: Machine Learning, Exception Handling, Python Programming, Python3

What is Exception and Exception Handling ?

Suppose you are travelling from your house to office but in the midway your car get punctured hence your car will stop this scenario is called Exception.Now Mechanic will come and repair your car and you will reach office and process of repairing the puncture is known as exception handling.

How dangerous is it not to use Exception Handling ?

In 1996 flight 501 resulted into Catastrophic Explosion and hundreds of people were killed,because the airline company has avoided exception handling policy of aborting computation on arithmetic error.

why programmer should use Exception Handling ?

i.> Without Exception Handling :- Let you have written a code and there is some runtime error in code then that error will not allow kernel to operate next set of code.

ii.> With Exception Handling:- Again there is some runtime error in code but with the help of Exception handling kernel can operate next set of code(avoiding code with some error).

How we can write it in Python ?

In Python except block comes with try block;

# syntax of code looks like

try:

#write the code you think is disasterous

#......................

#......................

Except Exception as e:

print(e) #it will print the kind of error present in code

If there will be any error in code then except block will be executed and if there will be no any error in code then try block will be executed.

#Take teo variable a,b print(a/b) run the code till both the value

#will be taken integer otherwise print the exception while True:

try:

a=int(input())

b=int(input())

c=a/b

print(c)

break

except:

print("Invalid input")

continue [out]>> 12

akhil

Invalid input

Anand

Invalid input

12

10

1.2

Can we use multiple Except block within single try block ?

yes we can use multiple except block within try block.These except block would be in hierarchy.

#using multiple except block within single try block try:

a=int(input())

print(a/0)

except IOError:

print("This is an i/0 error")

except ValueError:

print("You have taken Invalid Error")

except ZeroDivisionError:

print("Error occured because variable is divided by Zero") [out]>> 12

Error occured because variable is divided by Zero

Can we use nested try ->Except block ?

yes , we can use nested try ->except statement but with certain observation.let if outer try block will fail it will go directly to the outer except block. If we want want to run inner try -> except statement then it is compulsory to be no error in outer try block code.

#nested try except try:

a=int(input())

b=int(input())

c=a/b

print(c)

try:

d=int(input())

print(d*2)

except:

print("You have taken invalid input")

except:

print("You should again check your input") [out]>> #when outer try statement fails

12

Akhil

You should again check your input

#when outer try statement run and inner try statement fails

12

2

6.0

Akhi

You have taken invalid input

Explain try -> except -> else -> finally

Else statement will be executed only when try statement will execute sucessfully.And finally statement has no such criteria if we will take finally ststement it will execute with every execution.

try:

a=int(input())

b=int(input())

c=a/b

print(c)

except:

print("You have given invalid credentials")

else:

print("it will be excuted when try block will execute")

finally:

print("It will execute every time") [out]>> #when try block will be executed sucessfully

34

12

2.8333333333333335

else will be excuted when try block will execute

finally will execute every time

#when try block will not be executed sucessfully

12

Akhil

You have given invalid credentials

finally will execute every time

can we create own exception ?

Yes we can create own exception which is known as custom exception.This can be done with the help and inheritance property.

#creating my own exception

#let's create any number divisible by 1 will give OneDivisionError class OneDivisionError(Exception):

pass

try:

a=int(input())

b=int(input())

c=a/b

print(c)

if b==1:

raise OneDivisionError

except OneDivisionError:

print("This is User created exception") [out]>> 12

1

12.0

This is User created exception

Note:-Exception Handling doesn’t work with syntactical errors.i.e:-it will not work with indentation error.Exception Handling majorly works on run time error.

References:-

iNeuron,stackoverflow