h apter
C
Exception Handling
1 in Python
“I like my code to be elegant and efficient. The logic
should be straightforward to make it hard for bugs to hide,
the dependencies minimal to ease maintenance, error handling
complete according to an articulated strategy, and performance
close to optimal so as not to tempt people to make the code messy
with unprincipled optimization. Clean code does one thing well.”
— Bjarne Stroustrup
In this Chapter
»» Introduction
»» Syntax Errors
»» Exceptions 1.1 Introduction
»» Built-in Exceptions Sometimes while executing a Python program, the
»» Raising Exceptions program does not execute at all or the program
executes but generates unexpected output or
»» Handling Exceptions
behaves abnormally. These occur when there are
»» Finally Clause syntax errors, runtime errors or logical errors in
the code. In Python, exceptions are errors that
get triggered automatically. However, exceptions
can be forcefully triggered and handled through
program code. In this chapter, we will learn about
exception handling in Python programs.
1.2 Syntax Errors
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. On encountering a
syntax error, the interpreter does not execute the
program unless we rectify the errors, save and
2024-25
Chapter 1.indd 1 18-Jun-21 2:27:3
, rerun the program. When a syntax error is encountered
while working in shell mode, Python displays the name
of the error and a small description about the error as
shown in Figure 1.1.
Figure 1.1: A syntax error displayed in Python shell mode
So, a syntax error is reported by the Python
interpreter giving a brief explanation about the error
and a suggestion to rectify it.
Similarly, when a syntax error is encountered while
running a program in script mode as shown in Figure
1.2, a dialog box specifying the name of the error (Figure
1.3) and a small description about the error is displayed.
Figure 1.2: An error in the script
Figure 1.3: Error dialog box
2 Computer Science - Class XII
2024-25
Chapter 1.indd 2 18-Jun-21 2:27:3
, 1.3 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 might
disrupt the normal execution of the program and are
called exceptions.
An exception is a Python object that represents an
error. When an error occurs during the execution of a
program, an exception is said to have been raised. Such
an exception needs to be handled by the programmer
so that the program does not terminate abnormally.
Therefore, while designing a program, a programmer
may anticipate such erroneous situations that may arise
during its execution and can address them by including
appropriate code to handle that exception.
It is to be noted that SyntaxError shown at Figures
1.1 and 1.3 is also an exception. But, all other exceptions
are generated when a program is syntactically correct.
1.4 Built-in Exceptions
Commonly occurring exceptions are usually defined
in the compiler/interpreter. These are called built-in
exceptions.
Python’s standard library is an extensive collection
of built-in exceptions that deals with the commonly
occurring errors (exceptions) by providing the
standardized solutions for such errors. On the occurrence
of any built-in exception, the appropriate exception
handler code is executed which displays the reason
along with the raised exception name. The programmer
then has to take appropriate action to handle it. Some
of the commonly occurring built-in exceptions that can
be raised in Python are explained in Table 1.1.
Table 1.1 Built-in exceptions in Python
Name of the Built-
S. No Explanation
in 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 or operation receives an argument
that has the right data type but mismatched or inappropriate values.
3. IOError It is raised when the file specified in a program statement cannot be
opened.
Exception Handling in Python 3
2024-25
Chapter 1.indd 3 18-Jun-21 2:27:3
C
Exception Handling
1 in Python
“I like my code to be elegant and efficient. The logic
should be straightforward to make it hard for bugs to hide,
the dependencies minimal to ease maintenance, error handling
complete according to an articulated strategy, and performance
close to optimal so as not to tempt people to make the code messy
with unprincipled optimization. Clean code does one thing well.”
— Bjarne Stroustrup
In this Chapter
»» Introduction
»» Syntax Errors
»» Exceptions 1.1 Introduction
»» Built-in Exceptions Sometimes while executing a Python program, the
»» Raising Exceptions program does not execute at all or the program
executes but generates unexpected output or
»» Handling Exceptions
behaves abnormally. These occur when there are
»» Finally Clause syntax errors, runtime errors or logical errors in
the code. In Python, exceptions are errors that
get triggered automatically. However, exceptions
can be forcefully triggered and handled through
program code. In this chapter, we will learn about
exception handling in Python programs.
1.2 Syntax Errors
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. On encountering a
syntax error, the interpreter does not execute the
program unless we rectify the errors, save and
2024-25
Chapter 1.indd 1 18-Jun-21 2:27:3
, rerun the program. When a syntax error is encountered
while working in shell mode, Python displays the name
of the error and a small description about the error as
shown in Figure 1.1.
Figure 1.1: A syntax error displayed in Python shell mode
So, a syntax error is reported by the Python
interpreter giving a brief explanation about the error
and a suggestion to rectify it.
Similarly, when a syntax error is encountered while
running a program in script mode as shown in Figure
1.2, a dialog box specifying the name of the error (Figure
1.3) and a small description about the error is displayed.
Figure 1.2: An error in the script
Figure 1.3: Error dialog box
2 Computer Science - Class XII
2024-25
Chapter 1.indd 2 18-Jun-21 2:27:3
, 1.3 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 might
disrupt the normal execution of the program and are
called exceptions.
An exception is a Python object that represents an
error. When an error occurs during the execution of a
program, an exception is said to have been raised. Such
an exception needs to be handled by the programmer
so that the program does not terminate abnormally.
Therefore, while designing a program, a programmer
may anticipate such erroneous situations that may arise
during its execution and can address them by including
appropriate code to handle that exception.
It is to be noted that SyntaxError shown at Figures
1.1 and 1.3 is also an exception. But, all other exceptions
are generated when a program is syntactically correct.
1.4 Built-in Exceptions
Commonly occurring exceptions are usually defined
in the compiler/interpreter. These are called built-in
exceptions.
Python’s standard library is an extensive collection
of built-in exceptions that deals with the commonly
occurring errors (exceptions) by providing the
standardized solutions for such errors. On the occurrence
of any built-in exception, the appropriate exception
handler code is executed which displays the reason
along with the raised exception name. The programmer
then has to take appropriate action to handle it. Some
of the commonly occurring built-in exceptions that can
be raised in Python are explained in Table 1.1.
Table 1.1 Built-in exceptions in Python
Name of the Built-
S. No Explanation
in 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 or operation receives an argument
that has the right data type but mismatched or inappropriate values.
3. IOError It is raised when the file specified in a program statement cannot be
opened.
Exception Handling in Python 3
2024-25
Chapter 1.indd 3 18-Jun-21 2:27:3