Class XI
Chapter 7 : Data Handling
Topic :- Debugging
1. Definition
Debugging is the process of identifying, locating, and removing errors (bugs) from a program so
that it runs correctly.
2. Types of Errors
Type of
When it Occurs Example Detected By
Error
Syntax During compilation or if x = 5 instead of if
Compiler/Interpreter
Error interpretation x == 5
Logical Program runs but gives Programmer during
Using + instead of *
Error wrong output testing
Runtime Division by zero, file not
While the program is running System during execution
Error found
3. Debugging Process
1. Identify the error (error messages, wrong output).
2. Locate where it occurs in the code.
3. Analyze the cause of the error.
4. Fix the code.
5. Retest to ensure the problem is solved.
4. Debugging Techniques
Manual Debugging: Reading through the code carefully.
Print/Trace Statements: Printing variable values to check program flow.
Using a Debugger Tool: Step-by-step execution, breakpoints, watching variables.
Code Review: Having another person check the code.
5. Difference Between Testing and Debugging
Testing Debugging
Finds the presence of errors Removes the errors
Performed by tester Performed by programmer
Answers “Is there an error?” Answers “Where and why is the error?”
Testing tells you something is wrong; Debugging tells you what is wrong and fixes it.
Error (in Python)
An error is a problem in a program that disrupts its normal execution and is usually beyond the
programmer’s control. Errors may be syntax errors (detected before execution) or runtime
errors (occur while running), and many cannot be recovered from.
Exception (in Python)
An exception is a type of runtime error that occurs during program execution due to invalid
operations or data, and can be handled in the program using try…except blocks to prevent the
program from crashing.
Daily Life Example of Error and Exception
Error (Unrecoverable problem)
Imagine you are writing a letter and your pen runs out of ink.
You cannot continue writing unless you replace the pen.
It stops your work completely → just like an error stops a program.
Exception (Recoverable problem)
Imagine you are driving to school and there is a traffic jam.
You take a different route and still reach school.
The problem happened, but you handled it → just like an exception that is caught and
handled so the program continues.
Built-in Exceptions in Python
Python has many predefined exception classes that are automatically raised when
certain errors occur during program execution. These help programmers detect and
handle problems without stopping the program.
Some Common Built-in Exceptions
Exception Name When It Occurs Example
ZeroDivisionError Division or modulo by zero 10 / 0
ValueError Invalid value given to a function int("abc")
TypeError
Operation or function applied to wrong data "5" + 3
type
IndexError
Accessing index that does not exist in a mylist[10]
list/tuple
mydict["name"] (if key
KeyError Accessing a non-existing key in a dictionary
missing)
FileNotFoundError File operation on a non-existing file open("abc.txt")
NameError Using a variable that is not defined print(x) (if x not defined)
AttributeError
Accessing an undefined attribute of an "hello".push()
object
ImportError Importing a module that does not exist import abcxyz
OverflowError
Arithmetic operation exceeds the limit for a Large number exponentiation
numeric type
EOFError (End Of File Error)
Definition:
Raised when the input() function (or similar read operation) hits end-of-file condition (no more
data to read) without reading any data.
IOError (Input/Output Error)
Definition:
Raised when an input/output operation (like reading/writing to a file) fails for an I/O-related
reason.
When it occurs:
File not found.
Disk full.
File not accessible.