Exception Handling in Python Class 12 Notes

Share with others

Exception Handling in Python Class 12 Notes: Exception handling is a method in Python that helps to handle runtime errors that come during the execution of a program. In Python, exception handling is done by using a try-except block.

Exception Handling in Python Class 12 Notes

What is error?

Errors are problems that come during the execution of the program. There are two types of error, syntax error and runtime error. Runtime error is handled through exception handling; each of the errors impacts the program’s behavior differently.

types of error in python

a. Syntax Error: Syntax errors are detected when we have not followed the rules of the particular programming language while writing a program. These errors are also known as parsing errors. If a syntax error occurs, then without rectifying the error, the program will not execute.

syntax error in python

b. Exceptions: Even if a statement or expression is syntactically correct, there might arise an error during its execution. For example, trying to open a file that does not exist, division by zero, and so on. Such types of errors can be handled in the time of execution using exception handling.

i. Run time error without exceptions

run time error without exceptions

ii. Run time error handled with exceptions

Exceptions handling in python

What is exception handling?

Exception handling is a method in Python that helps to handle runtime errors that come during the execution of a program. In Python, exception handling is done by using a try-except block. It involves identifying the potential errors, handling them properly, and allowing the program to execute even if any exception is encountered.

Need for Exception Handling

Exception handling is a useful technique that helps in capturing runtime errors and handling them so as to avoid the program crashing. Following is some of the important points regarding exceptions and their handling:

  • Python provides a wide range of built-in exceptions like ValueError, ZeroDivisionError, etc.
  • Python provides a separate block for logic and error handling.
  • Python helps the developers quickly debug issues and identify problem areas.

How to use exception handling using try and except Block

When an error occurs, the Python interpreter creates an object called the exception object. This object contains information about the error, like its type, file name, and position in the program where the error has occurred. To handle this error, the following exception handling code is required:

How to use exception handling using try and except Block

Built-in exceptions in Python

Name of the Built in ExceptionExplanation
SyntaxErrorIt is raised when there is an error in the syntax of the Python code.
ValueErrorIt is raised when a built-in method or operation receives an argument that has the right data type but mismatched or inappropriate values.
IOErrorIt is raised when the file specified in a program statement cannot be opened.
KeyboardInterruptIt 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.
ImportErrorIt is raised when the requested module definition is not found.
EOFErrorIt is raised when the end of file condition is reached without reading any data by input().
ZeroDivisionErrorIt is raised when the denominator in a division operation is zero.
IndexErrorIt is raised when the index or subscript in a sequence is out of range.
NameErrorIt is raised when a local or global variable name is not defined.
IndentationErrorIt is raised due to incorrect indentation in the program code.
TypeErrorIt is raised when an operator is supplied with a value of incorrect data type.
OverFlowErrorIt is raised when the result of a calculation exceeds the maximum limit for numeric data type.

Catching Exceptions

The try block is handled in the except block. If an exception occurs within the try block, the execution immediately jumps to the except block. The except block specifies how to handle the exception. If no exception occurs in the try block, the except block is skipped.

Catching Exceptions

Example of simple program with multiple exception handling blocks:

exception handling using except clause

try…except…else clause

We can put an optional else clause along with the try…except 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.

try except else clause

Example of simple program with multiple exception handling blocks:

exception handling using else clause

Finally clause

The try statement in Python can also have an optional finally clause. The statements inside the finally block are always executed regardless of whether an exception has occurred in the try block or not. It is a common practice to use finally clause while working with files to ensure that the file object is closed.

How to use exception handling using try and except Block

Example of simple program with multiple exception handling blocks:

exception handling using finally clause

How to handle Multiple Errors

To handle multiple exceptions in Python, you can use a single try and multiple except blocks in Python. This helps to handle different types of errors that may occur during the runtime. Every except block is associated with a specific exception type.

How to handle Multiple Errors

Example of simple program with multiple exception handling blocks:

example how to handle multiple errors

Disclaimer: We have taken an effort to provide you with the accurate handout of “Exception Handling in Python Class 12 Notes“. If you feel that there is any error or mistake, please contact me at anuraganand2017@gmail.com. The above CBSE study material present on our websites is for education purpose, not our copyrights. All the above content and Screenshot are taken from Computer Science Class 12 CBSE Textbook, Sample Paper, Old Sample Paper, Board Paper, NCERT Textbook and Support Material which is present in CBSEACADEMIC website, This Textbook and Support Material are legally copyright by Central Board of Secondary Education. We are only providing a medium and helping the students to improve the performances in the examination. 

Images and content shown above are the property of individual organizations and are used here for reference purposes only.

For more information, refer to the official CBSE textbooks available at cbseacademic.nic.in

cbseskilleducation


Share with others

Leave a Comment