Exception Handling
Exception Handling
Introduction
Errors in python can be of two types i.e. Syntax errors and Exceptions.
Syntax errors
Syntax errors or parsing errors are detected when we have not followed the rules
of the particular programming language while writing a program.
When syntax error is encountered, Python displays the name of the error and a
small description about the error.
The execution of the program will start only after the syntax error is rectified.
Exceptions
An exception is a Python object that represents an error.
Syntax errors are also handled as exceptions.
The exception needs to be handled by the programmer so that the program does
not terminate abruptly.
When an exception occurs during execution of a program and there is a built-in
exception defined for that, the error message written in that exception is displayed.
The programmer then has to take appropriate action and handle it.
Some of the commonly occurring built-in exceptions are SyntaxError, ValueError,
IOError, KeyboardInterrupt, ImportError, EOFError, ZeroDivisionError, IndexError,
NameError, IndentationError, TypeError,and OverFlowerror.
Table: Built-in exceptions in Python.
S.No Name of the Built- Explanation
in Exception
1 SyntaxError It is raised when there is an error in the syntax of the Python
code.
2 ValueError It is raised when a built-in method or operation receives an
argument that has the right data type but mismatched or
inappropriate values.
3 IOError It is raised when the file specified in a program statement
cannot be opened.
4 KeyboardInterrupt It is raised when the user accidentally hits the Delete or Esc
key while executing a program due to which the normal flow
of the program is interrupted.
5 ImportError It is raised when the requested module definition is not
found.
1
6 EOFError It is raised when the end of file condition is reached without
reading any data by input()
7 ZeroDivisionError It is raised when the denominator in a division operation is
zero.
8 IndexError It is raised when the index or subscript in a sequence is out
of range.
9 NameError It is raised when a local or global variable name is not
defined.
10 IndentationError It is raised due to incorrect indentation in the program code.
11 TypeError It is raised when an operator is supplied with a value of
incorrect data type.
12 OverFlowError It is raised when the result of a calculation exceeds the
maximum limit for numeric data type.
2
Example: Using try..except block
try:
x=int(input("x="))
y=int(input("y="))
z=x/y
print("Division performed successfully")
except ZeroDivisionError:
print("Denominator as ZERO…..not allowed")
Output:
X=5
Y=0
Denominator as ZERO…..not allowed
Example: Using of multiple except clauses
try:
x=int(input("x="))
y=int(input("y="))
z=x/y
print("Division performed successfully")
except ZeroDivisionError:
print("Denominator as ZERO…..not allowed")
except ValueError:
print("Only INTEGERS should be entered")
try…except…else clause
An except block will be executed only if some exception is raised in the try block.
But if there is no error then none of the except blocks will be executed.
In this case, the statements inside the else clause will be executed.
3
Example: Use of else clause
try:
x=int(input("x="))
y=int(input("y="))
z=x/y
print("Division performed successfully")
except ZeroDivisionError:
print("Denominator as ZERO…..not allowed")
except ValueError:
print("Only INTEGERS should be entered")
else:
print(“The result of division operation is”,z)
finally clause
The statements inside the finally block are always executed regardless of whether
an exception occurred in the try block or not.
Example: Use of finally clause
try:
x=int(input("x="))
y=int(input("y="))
z=x/y
print("Division performed successfully")
except ZeroDivisionError:
print("Denominator as ZERO…..not allowed")
except ValueError:
print("Only INTEGERS should be entered")
else:
print(“The result of division operation is”,z)
finally:
print(“OPERATION SUCESSFUL”)