You are on page 1of 16

Exception Handling

Exception

• Exception can be said to be any abnormal condition in a program


resulting to the disruption in the flow of the program.

• Whenever an exception occurs the program halts the execution and


thus further code is not executed. Thus exception is that error which
python script is unable to tackle with.
Common Exceptions

• ZeroDivisionError: Occurs when a number is divided by zero.

• NameError: It occurs when a name is not found. It may be local or


global.

• IndentationError: If incorrect indentation is given.

• IOError: It occurs when Input Output operation fails.

• EOFError: It occurs when end of the file is reached and yet operations
are being performed.
Exceptions
▪ Whenever a runtime error occurs, it creates an exception. Usually,
the program stops and Python prints an error message.
• For example,

>>> print 55/0


ZeroDivisionError: integer division or modulo

>>> a = []
>>> print a[5]
IndexError: list index out of range

>>> b = {}
>>> print b[’what’]
KeyError: what

>>> f = open("Idontexist", "r")


IOError: [Errno 2] No such file or directory: ’Idontexist’
Handling Exception

try:
Statements
Except ExceptionName:
Statments
Example 1

a=int(input("a="))
b=int(input("b="))
try:
c=a/b
print(c)
except:
print("Inputs are not valid")
Multi Except Block
try:
Statements
Except ExceptionName1:
Statments
Except ExceptionName2:
Statments
Except ExceptionNamen:
Statments
Else:
Statements
try:
code
except Exception1,Exception2,Exception3,..,ExceptionN
execute this code in case any Exception of these occur.
else:
execute code in case no exception occurred.
Example 2

try:
a=int(input("a="))
b=int(input("b="))
print(a/b)
except(ZeroDivisionError):
print("Inputs are not correct")
except(ValueError):
print("Only number should be entered")

print("END")
Example 3

try:
num = int(input("enter a number"))
print(num**2)

except (KeyboardInterrupt):
print("You should have entered a number")
except (ValueError):
print("Please check before you enter")
Finally
a = 10
b=0
try:
print("Resources open")
print(a/b)
#print("Resources closed")

except Exception:
print("Something went wrong")
#print("Resources closed")

finally:
print("Resources closed")
raise

try:
marks = int(input("enter your marks"))
if marks<0 or marks>100:
raise Exception(marks)
print("marks within valid range",marks)

except Exception as e:
print("error, invalid marks input",e)
Raise example
a=10
#b=2
b=10
try:
print(a/b)
raise Exception(a/b)
print("aa")

except Exception as e:
print("You can't divide by zero:",e)
print("Bye")
Example 4

a=10
#b=2
b=0
try:
print(a/b)

except Exception as e:
print("You can't divide by zero:",e)
print("Bye")
a=10
b=2
try:
print(a/b)
m = int(input("enter a number"))
print(m)
m = n +10
print(m)
except ZeroDivisionError:
print("You can't divide by zero:",e)
except ValueError:
print("enter valid number")
except NameError:
print("Name 'n' is not defined")

print("Bye")
Example 5(if there is no exception)

try:
a=10/0
except:
print("Arithmetic Exception")
else:
print("Successfully Done")
Example 6

try:
a=10/0;
except ArithmeticError,StandardError:
print "Arithmetic Exception"
else:
print "Successfully Done"

You might also like