The Bombay Salesian Society’s
Don Bosco Institute of Technology, Kurla(W), Mumbai
Department of Basic Science and Humanities
VSEC202 Python Programming FE_COMP (NEP)
A.Y. 2024-25 SEM II
Experiment 5
Title: Exception Handling and Debugging in Python
Objective: To enable learners to proficiently handle errors and exceptions in Python programs,
ensuring robust and fault-tolerant code. Learners will also develop debugging skills to identify,
diagnose, and fix issues efficiently using scientific debugging methods.
Course Outcome: VSEC202.3 - Develop Python programs to perform operations on data
structures, manage files, handle exceptions, and create modular applications using functions and
libraries.
Learning Outcome: To develop Python programs with robust exception handling and
debugging skills, ensuring error-free and efficient code.
Theory:
Explain Exception Handling meaning and syntax of Try-except block and finally block.
IT is a mechanism in programming that allows developers to manage errors or exceptional
conditions that may occur during the execution of a program. Instead of crashing the program
when an error occurs, exception handling provides a way to gracefully handle the error,
allowing the program to continue running or to terminate in a controlled manner
Syntax of try except
try:
# Code that may raise an exception
risky_operation()
except SomeException as e:
# Code that runs if the specified exception occurs
handle_error(e)
Syntax of finally block:
try:
# Code that may raise an exception
risky_operation()
except SomeException as e:
# Code that runs if the specified exception occurs
handle_error(e)
finally:
# Code that will run no matter what
cleanup_operation()
The Bombay Salesian Society’s
Don Bosco Institute of Technology, Kurla(W), Mumbai
Department of Basic Science and Humanities
VSEC202 Python Programming FE_COMP (NEP)
A.Y. 2024-25 SEM II
Explain the concept of debugging, types of bugs, and the importance of using a debugger.
Demonstrate how to use a Python debugger (e.g., pdb or an IDE with debugging
capabilities) to set breakpoints, step through code, and examine variable values.
It is the process of identifying, isolating, and fixing problems or "bugs" in a computer program.
Bugs can arise from various sources, including logical errors, syntax errors, and runtime errors.
Debugging is a critical skill for developers, as it helps ensure that software functions as intended
and meets user requirements.
Types of Bugs
Syntax Errors: These occur when the code violates the rules of the programming language. For
example, missing a colon or parentheses.
Example: print("Hello World" (missing closing parenthesis)
Runtime Errors: These occur during the execution of the program, often due to invalid
operations, such as dividing by zero or accessing an out-of-bounds index in a list.
Example: result = 10 / 0 (division by zero)
Logical Errors: These occur when the program runs without crashing, but the output is not what
the programmer intended. This can be due to incorrect algorithms or faulty logic.
Example: Using the wrong formula to calculate a result.
Semantic Errors: These occur when the code is syntactically correct but does not produce the
expected outcome due to incorrect assumptions or misunderstandings of the problem.
Example: Misunderstanding the requirements of a function.
Importance of Using a Debugger
1. Identifying Bugs: Debuggers allow developers to pinpoint the exact location of a bug in
the code.
2. Stepping Through Code: Developers can execute code line by line to observe the flow
of execution and the state of variables.
3. Examining Variable Values: Debuggers provide the ability to inspect the values of
variables at different points in the execution.
4. Setting Breakpoints: Developers can pause execution at specific lines to analyze the
program's state.
5. Improving Code Quality: By using a debugger, developers can better understand their
code, leading to improved quality and maintainability.
Using pdb to Debug the Program
The Bombay Salesian Society’s
Don Bosco Institute of Technology, Kurla(W), Mumbai
Department of Basic Science and Humanities
VSEC202 Python Programming FE_COMP (NEP)
A.Y. 2024-25 SEM II
1. Set Up the Debugger: Add the following line to set a breakpoint before
the calculate_average function is called:
import pdb; pdb.set_trace()
2. Run the Program: Execute the program in your terminal or command prompt:
Python3 your_script.py
3. Using the Debugger:
When the program hits pdb.set_trace(), it will pause execution and give you
a (Pdb) prompt.
You can use the following commands to navigate through the code:
l (list): Show the current location in the code.
n (next): Execute the next line of code.
c (continue): Continue execution until the next breakpoint.
s (step): Step into a function call.
p variable_name: Print the value of a variable.
q (quit): Exit the debugger
Program 5.1: Basic Exception Handling
Aim: Write a Python program that takes two numbers as input and performs division. Implement
exception handling to manage division by zero and invalid input errors gracefully.
Code Screenshots:
(Attach screenshots of the implemented Python code.)
The Bombay Salesian Society’s
Don Bosco Institute of Technology, Kurla(W), Mumbai
Department of Basic Science and Humanities
VSEC202 Python Programming FE_COMP (NEP)
A.Y. 2024-25 SEM II
Output Screenshots:
(Attach screenshots of the program output, ensuring that the output contains your roll
number and name.)
Conclusion:
Write a brief summary of your learnings from this experiment, including insights gained in
exception handling and debugging.
1. Importance of Exception Handling: It allows programs to manage errors gracefully,
improving user experience and preventing crashes.
2. Try-Except Structure: The try-except block separates normal code from error handling,
enabling specific responses to different types of exceptions.
3. Finally Block: Ensures that cleanup code runs regardless of whether an exception
occurred, maintaining resource integrity.
4. Understanding Bugs: Recognizing different types of bugs (syntax, runtime, logical) is
crucial for effective debugging.
The Bombay Salesian Society’s
Don Bosco Institute of Technology, Kurla(W), Mumbai
Department of Basic Science and Humanities
VSEC202 Python Programming FE_COMP (NEP)
A.Y. 2024-25 SEM II
Program 5.2: Using a Debugger
Aim: Demonstrate the use of a Python debugger (e.g., pdb or an IDE with debugging
capabilities) on a sample program with intentional errors. Guide students on setting breakpoints,
stepping through code, and examining variable values.
Code Screenshots:
(Attach screenshots of the implemented Python code.)
Output Screenshots:
(Attach screenshots of the program output, ensuring that the output contains your roll
number and name.)
The Bombay Salesian Society’s
Don Bosco Institute of Technology, Kurla(W), Mumbai
Department of Basic Science and Humanities
VSEC202 Python Programming FE_COMP (NEP)
A.Y. 2024-25 SEM II
Conclusion:
Write a brief summary of your learnings from this experiment, including insights gained in
exception handling and debugging.
Through this debugging exercise, I learned the importance of setting breakpoints to pause execution and
inspect variable states at critical points in the program. Utilizing the pdb debugger allowed for a step-by-
step examination of the code flow, which is invaluable for identifying logical errors and understanding
exception handling. The ability to print variable values and navigate through the code helped clarify how
inputs affect outputs, reinforcing the need for robust error handling to manage unexpected user inputs
effectively. Overall, this experience highlighted the significance of debugging as a crucial skill in software
development, enabling the identification and resolution of issues efficiently.