0% found this document useful (0 votes)
4 views3 pages

Notes - Exception Handling in Python

The document explains exception handling in Python, detailing how exceptions are errors that occur during program execution and can be managed using try and except clauses. It lists common built-in exceptions such as SyntaxError, ValueError, and ZeroDivisionError, providing explanations for each. Additionally, it includes examples demonstrating the use of try, except, else, and finally blocks in handling exceptions.

Uploaded by

Arsheya Arshida
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Notes - Exception Handling in Python

The document explains exception handling in Python, detailing how exceptions are errors that occur during program execution and can be managed using try and except clauses. It lists common built-in exceptions such as SyntaxError, ValueError, and ZeroDivisionError, providing explanations for each. Additionally, it includes examples demonstrating the use of try, except, else, and finally blocks in handling exceptions.

Uploaded by

Arsheya Arshida
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

EXCEPTION HANDLING IN PYTHON

Exception
An exception is an error that occurs during the execution of a program. Whenever, there is an error,
Python generates an exception that could be handled.
The exception handling in python involves the use of try and except clauses.

try:
#write here the code that may generate an exception
Except [exception name]:
#write code here about what to do the exception has occurred

Built – in Exceptions
Commonly occurring exceptions are usually defined in the compiler/interpreter. These are called built-in
exceptions. Built-in exceptions deals with the commonly occurring errors by providing the standardized
solutions for such errors.

Python comes with built in exceptions.


Sno Name of built in Explanation
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 receives an argument that has the right
data type but mismatched 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.
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 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.

EOFError : raised when one of the built in functions hits an end-of-file condition (EOF) without reading
any data.

Eg : 1
print("Practising for try block") Practising for try block
try : Enter the denominator =12
nr = 50 Division performed successfully
dr = int(input("Enter the denominator =")) OUTSIDE try …. Except block
Q = nr / dr
print("Division performed successfully")
except ZeroDivisionError:
print("Denominator as ZERO …… not allowed")
print("OUTSIDE try …. Except block")
Eg : 2
print("Practising for try block") Practising for try block
try : Enter the denominator =0
nr = 50 Denominator as ZERO …… not allowed
dr = int(input("Enter the denominator =")) OUTSIDE try …. Except block
Q = nr / dr
print("Division performed successfully")
except ZeroDivisionError:
print("Denominator as ZERO …… not allowed")
print("OUTSIDE try …. Except block")

Eg : 3
NOTE : We can have multiple except blocks for a single try block.
print("Handling multiple excetions") Handling multiple exceptions
try : Enter the denominator =25.5
nr = 50 Only integers should be entered
dr = int(input("Enter the denominator ="))
Q = nr / dr
print("Division performed successfully")
except ZeroDivisionError:
print("Denominator as ZERO …… not allowed")
except ValueError:
print("Only integers should be entered")

Eg : 4
NOTE : Use of except without specifying an exception
print("Handling exceptions without naming them") Handling exceptions without naming them
try : Enter the denominator =0
nr = 50 OOPS ...... some exception raised
dr = int(input("Enter the denominator ="))
Q = nr / dr
print("Division performed successfully")
except ValueError:
print("Only integers should be entered")
except :
print("OOPS ...... some exception raised")

Eg : 5
NOTE : Use of else clause (it is optional)
An except block will be executed 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.
print("Handling exception using try…. except….else") Handling exception using try….
try : except….else
nr = 50 Enter the denominator =20
dr = int(input("Enter the denominator =")) Division performed successfully
Q = nr / dr The result of division operation is 2.5
print("Division performed successfully")
except ValueError:
print("Only integers should be entered")
except ZeroDivisionError:
print("Denominator as Zero is not allowed")
else:
print("The result of division operation is ", Q )
finally clause (it is optional)
The statements inside the finally block are always executed regardless of whether an exception occurred
in the try block or not.

Eg:6
print("Handling exception using O/P
try…except….else..finally") Handling exception using
try : try…except….else..finally
nr = 50 Enter the denominator =12
dr = int(input("Enter the denominator =")) Division performed successfully
Q = nr / dr The result of division operation is
print("Division performed successfully") 4.166666666666667
except ValueError: OVER AND OUT
print("Only integers should be entered")
except ZeroDivisionError: O/P
print("Denominator as Zero is not allowed") Handling exception using
else: try…except….else..finally
print("The result of division operation is ", Q ) Enter the denominator =15.5
finally: Only integers should be entered
print(" OVER AND OUT") OVER AND OUT

Eg :7
print("Handling exception using O/P
try…except….else..finally") Handling exception using
try : try…except….else..finally
nr = 50 Enter the denominator =12.5
dr = int(input("Enter the denominator =")) OVER AND OUT
Q = nr / dr Traceback (most recent call last):
print("Division performed successfully") File "C:/Users/Hp/[Link]", line 4, in
except ZeroDivisionError: <module>
print("Denominator as Zero is not allowed") dr = int(input("Enter the denominator ="))
else: ValueError: invalid literal for int() with base
print("The result of division operation is ", Q ) 10: '12.5'
finally:
print(" OVER AND OUT")
NOTE : If the exception is not handled by any of the except clauses, then it is re-raised after the
execution of the finally block.

You might also like