Exception Handling in Python
Exception Handling in Python
INTRODUCTION:
Error in Python can be of two types i.e. Syntax errors and Exceptions.
Errors are problems in a program due to which the program will stop the
execution. On the other hand, exceptions are raised when some internal
events occur which change the normal flow of the program.
TYPES OF ERRORS:
(i)Compile-time errors. These are the errors resulting out of violation of
programming language’s grammar rules. All syntax errors are reported during
compilation.
(ii) Run-time errors. The errors that occur during runtime because of unexpected
situations. Such errors are handled through exception handling routines of Python.
Syntax Error:
These are the errors resulting out of violation of programming language’s grammar
rules. All syntax errors are reported during compilation. Example:
Exception: Exceptions are unexpected events or errors that occur during the
execution of a program, such as a division by zero or accessing an invalid memory
location. These events can lead to program termination or incorrect results.
“It is an exceptional event that occurs during runtime and causes normal
program flow to be disrupted.”
Observe that there is nothing wrong with the program syntax, it is only when we try
to divide an integer with zero, an exception is generated.
Exception (Examples):
Example 1:
\
Example 2:
Exception (Example-1):
Write a program to ensure that an integer
is entered as input and in case any other
value is entered, it displays a message –
‘Not a valid integer’
Syntax:
Execution Order:
The <try suite> is executed first ; if, during the course of executing the <try suite>,
an exception is raised that is not handled otherwise, and the <except suite> is
executed, with <name> bound to the exception, if found ; if no matching except
suite is found then unnamed except suite is executed.
finally Block:
The finally block is a part of the try-except block in Python that contains the code
that is executed regardless of whether an exception is raised or not. The syntax of
the try-except-finally block is as follows:
The try block contains the code that may raise an exception. If an exception occurs,
the control is transferred to the corresponding except block, which contains the
code to handle the exception. The finally block contains the code that is executed
after the try-except blocks, regardless of whether an exception occurred or not.
RAISE AN EXCEPTION:
Each time an error is detected in a program, the Python interpreter raises (throws) an
exception.
Exception handlers are designed to execute when a specific exception is raised.
Programmers can also forcefully raise exceptions in a program using the raise and assert
statements.
Once an exception is raised, no further statement in the current block of code is executed. So,
raising an exception involves interrupting the normal flow execution of program and jumping
to that part of the program (exception handler code) which is written to handle such
exceptional situations.
Raise Statement:
The raise statement can be used to throw an exception.
Syntax:
raise exception-name[(optional argument)]
The argument is generally a string that is displayed when the exception is raised. For example,
when an exception is raised as shown in below Figure, the message “OOPS : An Exception has
occurred” is displayed along with a brief description of the error.
The error detected may be a built-in exception or may be a user-defined one. Consider the
example given in below figure that uses the raise statement to raise a built-in exception called
IndexError.
Note: In this case, the user has only raised the exception but has not displayed any error message
explicitly.
In above figure, since the value of variable length is greater than the length of the list numbers,
an IndexError exception will be raised. The statement following the raise statement will not be
executed. So the message “NO EXECUTION” will not be displayed in this case
As we can see in above Figure, in addition to the error message displayed, Python also displays a
stack Traceback. This is a structured block of text that contains information about the sequence
of function calls that have been made in the branch of execution of code in which the exception
was raised. In the above figure, the error has been encountered in the most recently called
function that has been executed.