Chapter - 4 Error Detection and Debugging
Chapter - 4 Error Detection and Debugging
DEBUGGING
-Nirali Purohit
Generally, the programs
developed in high level
language like C, C++, Java
etc.,cannot understand by
the computer . It can
understand only low level
language. So, the program
written in high level
language to be converted
into low level language to
make it understandable for
the computer. This
conversion is performed
using either Interpreter or
Compiler.The basic flow of
any program execution is
shown in diagram.
In python IDLE program is executed from run module option of run menu
Debugging means the process of finding errors, finding reasons of
errors and techniques of their fixation.
An error, also known as a bug, is a programming code that prevents a
program from its successful interpretation.
Errors are of three types –
• Compile Error
• Run Time Error
• Logical Error
Compile time error :
These errors are basically of 2 types –
Syntax Error :Violation of formal rules of a programming language results in syntax error.
For ex:
len('hello') = 5
Semantics Error: Semantics refers to the set of rules which sets the meaning of
statements. A meaningless statement results in semantics error.
For ex:
x+y=z
SyntaxError: can't assign to operator
Logical Error
If a program is not showing any compile time error or run time
error but not producing desired output, it may be possible that
program is having a logical error.
Some example-
• Use a variable without an initial value.
• Provide wrong parameters to a function
• Use of wrong operator in place of correct operator required
for operation
x=a+b (here – was required in place of + as per requirement
Run time Error
These errors are generated during a program execution due to
resource limitation.
Python is having provision of checkpoints to handle these errors.
For ex
a=10
b=int(input(“enter a number”))
c=a/b
TypeError
This exception generates due to wrong type used with an operator or a function.
ValueError This exception generates due to wrong argument passed to a function.
ZeroDivisionError
This exception generates when divisor comes to zero.
OverFlowError This exception generates when result of a mathematical calculation exceeds the
limit.
KeyError This exception generates due to non-availability of key in mapping of dictionary.
FOFError This exception generates when end-of-file condition comes without reading
input of a built in function.
In python debugging can be done through
• Print line debugger
• Debugging tool
Print line debugger
• Python Debugger: pdb
– insert the following in your program to set a breakpoint
– when your code hits these lines, it’ll stop running and launch an interactive prompt for you to
inspect variables, step through the program, etc.
Create a.py file with below code and run it in python use n to step
next line.
num_list = [500, 600, 700]
alpha_list = ['x', 'y', 'z']
import pdb
import pdb
pdb.set_trace()
pdb.set_trace() #debugging code • n to step to the next line in
def nested_loop(): the current function
for number in num_list: • s to step into a function
print(number) for • c to continue to the next
letter in alpha_list: breakpoint
print(letter)
if __name__ == '__main__':
nested_loop()
ON
Open our program from file menu and run it:
It will than shown like this in debugger.
Click on STEP button for each line execution one by one and result
will be displayed in output window. When we will get wrong
value, we can stop the program there and can correct the code.