Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Summary

Summary Exception handling in python code

Rating
-
Sold
-
Pages
18
Uploaded on
16-04-2025
Written in
2024/2025

Notice all the lines for your successful way.

Institution
Course

Content preview

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

Written for

Institution
Course

Document information

Uploaded on
April 16, 2025
Number of pages
18
Written in
2024/2025
Type
SUMMARY

Subjects

$8.49
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
artwaygalaxy

Get to know the seller

Seller avatar
artwaygalaxy Seventhday adventist matric.hr.sec.school
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
2
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions