The document discusses exception handling in Python programs. It explains that when a runtime error occurs, an exception object is created and passed to the runtime system. The runtime system then searches the call stack for an exception handler to handle the error. If a matching handler is found, it is executed to catch the exception. Otherwise, program execution stops. The document provides an example Python code that uses an assert statement to check for negative numbers. When a negative number is passed, it results in an AssertionError being raised and the error message "OOPS.... Negative Number" being displayed.
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 ratings0% found this document useful (0 votes)
180 views3 pages
Lecs101 7 9
The document discusses exception handling in Python programs. It explains that when a runtime error occurs, an exception object is created and passed to the runtime system. The runtime system then searches the call stack for an exception handler to handle the error. If a matching handler is found, it is executed to catch the exception. Otherwise, program execution stops. The document provides an example Python code that uses an assert statement to check for negative numbers. When a negative number is passed, it results in an AssertionError being raised and the error message "OOPS.... Negative Number" being displayed.
value of the variable number. In case the number gets a negative value, AssertionError will be thrown, and subsequent statements will not be executed. Hence, on passing a negative value (-350) as an argument, it results in AssertionError and displays the message “OOPS…. Negative Number”. The output of the code is shown in Figure 1.7.
1.6 HANDLING EXCEPTIONS
Each and every exception has to be handled by the programmer to avoid the program from crashing abruptly. This is done by writing additional code in a program to give proper messages or instructions to the user on encountering an exception. This process is known as exception handling. 1.6.1 Need for Exception Handling Exception handling is being used not only in Python programming but in most programming languages like C++, Java, Ruby, etc. It is a useful technique that helps in capturing runtime errors and handling them so as to avoid the program getting crashed. Following are some
EXCEPTION HANDLING IN PYTHON 7
2022-23
Chapter 1.indd 7 18-Jun-21 2:27:39 PM
of the important points regarding exceptions and their handling: • Python categorises exceptions into distinct types so that specific exception handlers (code to handle that particular exception) can be created for each type. • Exception handlers separate the main logic of the program from the error detection and correction code. The segment of code where there is any possibility of error or exception, is placed inside one block. The code to be executed in case the exception has occurred, is placed inside another block. These statements for detection and reporting the exception do not affect the main logic of the program. • The compiler or interpreter keeps track of the exact position where the error has occurred. • Exception handling can be done for both user-defined and built-in exceptions.
1.6.2 Process of Handling Exception
When an error occurs, 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. A runtime system The object is handed over to the runtime system so that refers to the it can find an appropriate code to handle this particular execution of the exception. This process of creating an exception object statements given in and handing it over to the runtime system is called the program. It is a throwing an exception. It is important to note that complex mechanism consisting of when an exception occurs while executing a particular hardware and program statement, the control jumps to an exception software that handler, abandoning execution of the remaining comes into action program statements. as soon as the program, written in The runtime system searches the entire program any programming for a block of code, called the exception handler that language, is put for can handle the raised exception. It first searches for execution. the method in which the error has occurred and the exception has been raised. If not found, then it searches the method from which this method (in which exception was raised) was called. This hierarchical search in reverse order continues till the exception handler is found. This entire list of methods is known as call stack. When a suitable handler is found in the call stack, it is executed by the runtime process. This process of
8 COMPUTER SCIENCE - CLASS XII
2022-23
Chapter 1.indd 8 18-Jun-21 2:27:39 PM
executing a suitable handler is known as catching the exception. If the runtime system is not able to find an appropriate exception after searching all the methods in the call stack, then the program execution stops. The flowchart in Figure 1.8 describes the exception handling process.
Figure 1.8: Steps of handling exception
1.6.3 Catching Exceptions
An exception is said to be caught when a code that is designed to handle a particular exception is executed. Exceptions, if any, are caught in the try block and