The concept of identifying the runtime error representation
class object which is created internally at the time of execution of the program,
receiving that object and assigning that object to the reference variable of
corresponding runtime error representation class is known as exception
handling. Exception handling is used to stop the abnormal termination whenever
runtime error is occurred. We can implement the exception handling in python by
using try and except block.
Try block – The statements which causes to runtime errors and other statements which depends on the execution of the runtime error occur statements are recommended to represent by using try block.
Try block – The statements which causes to runtime errors and other statements which depends on the execution of the runtime error occur statements are recommended to represent by using try block.
Syntax –
try:
----
----At the time of execution of the try block statements, if any runtime error representation class object is created then immediately that object is identified by the try block, received by the try block and forward it to the except block without executing remaining statements of the try block.
Note – If there is no runtime error occurs at the time of execution of the try block then control will not go to the except block.
Except block – Except block should be preceded by the try block. Except block receives the runtime error representation class object which is given by the try block and assigns that object to the reference variable of corresponding runtime error representation class. In except block we can define the statements to display the user friendly error message.
Example –
print 'hi' i=input('Enter num1 : ') j=input('Enter num2 : ') try: k=i/j print k except(ZeroDivisionError): print 'num2 cannot be zero' print 'bye'
Output –
hi
Enter
num1 : 10
Enter
num2 : 0
num2
cannot be zero
bye
Single try with multiple except blocks – Whenever runtime error is occurred while executing try block statements then control will go to the first except block. If the first except block is not able to handle that runtime error then control will go to the second except block. If all except blocks cannot handle that runtime error then program will be terminated abnormally.
Single try with multiple except blocks – Whenever runtime error is occurred while executing try block statements then control will go to the first except block. If the first except block is not able to handle that runtime error then control will go to the second except block. If all except blocks cannot handle that runtime error then program will be terminated abnormally.
Example –
print 'hi' try: i = input('Enter num1 : ') j = input('Enter num2 : ') k=i/j print k
except(ZeroDivisionError): print 'num2 cannot be zero'
except(NameError): print 'Do not enter string'
print 'bye'
Output –
hi
Enter
num1 : 10
Enter
num2 : abc
Do not
enter string
byeDefault except block – Default block is used to handle any type of runtime error. In default except block we write the statements to display the common error messages (General error messages).
Example –
print 'hi' try: i = raw_input('Enter num1 : ') j = raw_input('Enter num2 : ') a=int(i) b=int(j) k=i/j print k
except(ZeroDivisionError): print 'num cannot be zero'
except(NameError): print 'do not enter string values'
except: print 'error occurred'
print 'bye'
Output –
hi
Enter
num1 : abc
Enter
num2 : 100
error
occurred
bye
Example –
print 'hi' try: i = raw_input('Enter num1 : ') j = raw_input('Enter num2 : ') a=int(i) b=int(j) k=i/j print k
except: print 'error occurred'
print 'bye
Output –
hi
Enter
num1 : 10
Enter
num2 : 0
error
occurred
byeNote – Default except block should be the last except block whenever we define single try with multiple except block, otherwise we will get syntax error.
Example –
print 'hi' try: i = raw_input('Enter num1 : ') j = raw_input('Enter num2 : ') a=int(i) b=int(j) k=i/j print k
except: print 'error occurred'
except(ZeroDivisionError): print 'num2 cannot be zero'
except(NameError): print 'Do not enter string value'
print 'bye'
Output –
File
"C:/Users/om/PycharmProjects/Pythonn/if3.py", line 8
print k
SyntaxError: default 'except:'
must be last
Finally block – The set
of statements which are compulsory to execute whether exception is occurred or
not occurred. Even though exception is occurred whether it is handled or not
handled recommended to represent in finally block. Resource releasing
statements (file closing statements, database connection closing statements)
are recommended to represent in finally block. Finally block should be preceded
by either try or except block.
Syntax 1 –
try :
--------
--------
finally
:
--------
--------
Syntax 2 –
try :
--------
--------
except()
:
-------
-------
finally
:
-------
-------
Example –
try : x=input("Enter num1 : ") y=input("Enter num2 : ") z=x/y print z
except(ZeroDivisionError): print 'Num2 cannot be zero'
finally: print 'end' print 'bye'
Output 1 –
Enter
num1 : 100
Enter
num2 : 20
5
end
bye
Output 2 –
Enter
num1 : 100
Enter
num2 : 0
Num2
cannot be zero
end
bye
Output 3 –
Enter
num1 : 100
Enter
num2 : abc
end
bye
Traceback
(most recent call last):
File
"C:/Users/om/PycharmProjects/Pythonn/if3.py", line 3, in
<module>
y=input("Enter num2 : ")
File "<string>", line 1, in
<module>
NameError: name 'abc' is
not defined
Nested try blocks – The concept
of defining one try block inside another try block is known as a nested try
block.
Syntax –
try :
----
----
try :
----
----
except :
----
----
except :
----
----
A try block which contains
an another try block is known as outer
try block. A try block which is defined in another try block is known as a inner try block. If exception is occurred
in the outer try block then control will go to the outer try except block. If
outer try except block is not able to handle that exception then program will
be terminated abnormally. If exception is occurred in inner try block then
control will go to the inner try except block. If inner try except block is not
able to handle that exception then control will go to the outer try except
block.
Example –
try: print 'in try1' try : print 'in try2' try : print 'in try3' except : print 'in except3' except : print 'in except2'
except : print 'in except1'
Output –
in try1
in try2
in try3
Example –
try : print 'in try1' try : x=input('Enter num1 : ') y=input('Enter num2 : ') z=x/y print z except(ZeroDivisionError): print 'Num2 cannot be zero'
except : print 'in except1'
Output 1 –
in try1
Enter
num1 : 10
Enter
num2 : 0
Num2
cannot be zero
Output 2 –
in try1
Enter
num1 : 10
Enter
num2 : abc
in
except1
Example –
try : print 'in try1' try : print 'in try2' except: print 'in except2' finally: print 'in finally2'
except : print 'in except1' try : print 'in try3' except : print 'in except3' finally: print 'in finally3'
finally: print 'in finally1'
try : print 'in try4'
except: print 'in except4'
finally: print 'in finally4'
Output –
in try1
in try2
in
finally2
in
finally1
in try4
in finally4
No comments:
Post a Comment