Exception
Handling
Introduction
ERRORS IN PYTHON AND DEBUGGING
The process of finding errors in a program is termed as Debugging.
Errors in Python are classified mainly into three types:
(a) Syntax Error
(b) Run-time Error
(c) Logical Error
Syntax Error
A syntax error is an error in the syntax of a sequence of characters or tokens that
is intended to be written in a particular programming language.
These types of errors are generated when we violate the syntax or, in other words,
the grammatical rules of a programming language.
Some examples of Python syntax errors are:
1. Incorrect indentation
2. Misspelling a keyword
3. Leaving out a symbol such as colon (:),
comma (,) or parentheses (()).
Run-time Error
A run-time error occurs after Python interpreter interprets the code you write
and the computer begins to execute it.
Some examples of Python run-time errors are:
1. Division by zero
2. Performing an operation on incompatible types
3. Using an identifier variable which has not been defined
4. Accessing a list element, dictionary value or object attribute which doesn’t exist
5. Trying to access a file that doesn’t exist
In Python, such unusual situations
are termed as Exceptions.
Exceptions are usually run-time
errors.
Logical Errors
A logical error/bug (called semantic error) does not stop execution but the
program behaves incorrectly and produces undesired/wrong output
The error is caused by a mistake in the program’s logic.
You won’t get an error message because no syntax or run-time error has
occurred.
Some examples of logical errors are:
• Using the wrong variable name for calculations.
• Using integer division or modulus operator in place of division operator
• Giving wrong operator precedence.
What is Exception Handling
Exception: An exception is an error that happens during the
execution of a program. If an exception is not caught, the program
is terminated.
Mechanism for handling an error when it occurs is called
Exception Handling
When an exception is raised on account of some error, the program
must contain code to catch this exception and handle it properly.
Exception refers to an abnormal condition that arises during the
program execution
Some important terms related to exception
handling:
(i) Traceback: The lengthy error message that is shown when an exception occurs
during the program run is called a traceback. The traceback gives information
regarding the line number(s) along with the function calls that caused the exception.
(ii) Try block: Try block constitutes a set of codes that might have an exception
thrown in it. If any code within the try statement causes an error, execution of the code
will stop and jump to the except statement.
(iii) ‘except’ block or ‘catch’ block: Whenever some error or exception is to be
handled, it is done using ‘except’ block which stands for exception.
(iv) ‘throw’ or ‘raise’ block: ‘throw’ is an action in response to an exception
(error/unusual condition). When a program on execution encounters an abnormal
condition, an object of this exception is created or instantiated and ‘thrown’ or ‘raised’
to the code responsible to catch/handle it.
(v) Unhandled, uncaught Exception: An exception that leads to abnormal
termination of a program due to non-execution of an exception thrown is termed as
unhandled or uncaught exception. This is the result of an exception which was thrown
or raised but never caught.
STANDARD EXCEPTIONS IN PYTHON
Python provides us a way to handle exceptions so that the script does not terminate but executes some
specified code when an exception occurs. This concept is called Exception Handling. And the code written to
handle it is known as exception handler.
For handling exceptional situations Python provides—
1. raise statement to raise exception in the program.
2. try... except statement for catching and handling errors
Common Python Exceptions
Error handling in Python involves the following steps:
Firstly, the moment an error occurs the state of execution of the program is saved.
• Normal flow of the program is interrupted (stopped for a moment).
• A special function or piece of code known as exception handler is executed.
• Execution of the program is resumed with the previously saved data.
Here are the different methods for handling exceptions in Python:
Method 1: try and except block
The try and except block in Python is used to catch and handle exceptions.
If you have some suspicious code that may raise an exception, you can defend your
program by placing the suspicious code in a try: block
If an error is encountered it is transferred down to the except block.
Method 2: try...except with else block
The else clause of try statement is used to specify the code which is executed in case no
exception occurs.
Method 2: try...except with else block
Method 3: try with multiple except blocks
The try statement may have multiple except blocks with Exception Names to
handle specific exceptions and one optional except clause
Method 3: try with multiple except blocks
Method 4: try...except with finally block
The finally block is called clean-up or termination clause because it is executed under all circumstances,
i.e., a “finally” block is always executed irrespective of whether an exception has occurred in a try block
or not.
Same as else block, this block is also an optional block but if there comes an exception it will still run.
Method 4: try...except with finally block