ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
HANDLING EXCEPTION
Exception
An Exception is an event which occurs during the execution of a program that disrupts the normal
flow of the program’s instructions. If a python script encounters a situation it cannot cope up, it
raises an exception
HOW TO HANDLE EXCEPTION?
There are four blocks which help to handle the exception. They are
try block
except statement
else block
finally block
i) try block
In the try block a programmer will write the suspicious code that may raise an
exception. One can defend their program from a run time error by placing the codes
inside the try block.
Syntax:
try:
#The operations here
ii) except statement
Except statement should be followed by the try block.
A single try block can have multiple except statement.
The except statement which handles the exception.
Multiple except statements require multiple exception names to handle the exception
separately.
except Exception 1:
#Handle Exception 1
except Exception 2:
#Handle Exception 2
iii) else block
If there is no exception in the program the else block will get executed.
Syntax:
else:
#If no Exception, it will execute
GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
iv) finally block:
A finally block will always execute whether an exception happens or not the block will
always execute.
finally:
#Always Execute
Syntax:
(i) Write a python program to write a file with exception handling.
(ii) Write a python program to read a file which raises an exception
Assume test.txt is not created in the computer and the following program is executed
which raises an exception.
GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
a) Except Clause with no exception
An except statement without the exception name, the python interpreter will consider
as default ‘Exception’ and it will catch all exceptions.
Syntax:
b) Except clause with multiple Exception
An except statement can have multiple exceptions, We call it by exception name
Syntax:
GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
(i) Write a python program to read a file with multiple exceptions
c) Argument of an Exception
An exception can have an argument which is a value that gives additional information
about the problem. The content of an argument will vary by the exception.
d) Raising an Exception
You can raise exceptions in several ways by using raise statement
Syntax:
Example:
GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
TYPES OF EXCEPTION
There are two types of Exception:
Built-in Exception
User Defined Exception
i) Built-in Exception
There are some built-in exceptions which should not be changed.
The Syntax for all Built-in Exception
except Exception_Name
GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
USER DEFINED EXCEPTION
In Python a user can create their own exception by deriving classes from standard
Exceptions. There are two steps to create a user defined exception.
Step-1
A User Defined Exception should be derived from standard Built-in Exceptions.
Step-2
After referring base class the user defined exception can be used in the program
GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING
ROHINI COLLEGE OF ENGINEERING & TECHNOLOGY
ASSERTION
An assertion is a sanity check which can turn on (or) turn off when the program is in testing mode.
The assertion can be used by assert keyword. It will check whether the input is valid and
after the operation it will check for valid output.
Syntax:
assert(Expression)
Example def add(x):
assert(x<0)
return(x)
add(10)
GE8151-PROBLEM SOLVING AND PYTHON PROGRAMMING