0% found this document useful (0 votes)
4 views4 pages

Debugging Programs

Debugging is the process of identifying and correcting errors in a program, which can be categorized into compile-time, run-time, and logical errors. Compile-time errors include syntax and semantic errors, while run-time errors occur during program execution and can lead to crashes. Exception handling is crucial for managing unexpected situations during execution, and tools like debuggers and code tracing help in effectively debugging programs.

Uploaded by

ajicejane1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Debugging Programs

Debugging is the process of identifying and correcting errors in a program, which can be categorized into compile-time, run-time, and logical errors. Compile-time errors include syntax and semantic errors, while run-time errors occur during program execution and can lead to crashes. Exception handling is crucial for managing unexpected situations during execution, and tools like debuggers and code tracing help in effectively debugging programs.

Uploaded by

ajicejane1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DEBUGGING PROGRAMS

What is debugging?
• Programmers find errors ,rectify them and recompile the code until the code is error
free .this process is called debugging.
• An error causing disruption in program running or in programming output is program
bug.
• Debugging refers to process of locating the place of errors,cause of error and correcting
the code
Errors in the program
• An error is anything that prevents a program from compiling and running.
3 types of error:
• Compile time error
• Run-time error
• Logical error
Compile time errors
• Errors that occurs during compile time .
• When a program compiles its source code it is checked for whether it follows
programming language rules or not
• 2 types of compile time errors
• Syntax errors:
when rules of programming language is violated
X<-y*z x=y*z
If(x=(x*y)) if(x==(x*y)):
• Semantic errors:
when statements are not meaningful
X*Y=Z
Run-time errors
• Errors that occurs during execution of the program. These are hard to detect.
• Some run time errors stops the execution of the program .this program is called crashed
or terminated abnormally
• Eg:infinite loop,or wrong value input(data type different)
Logical errors
• Program does not provide correct result. This is because logic of the program may
not be correct.
• Eg:incorrectly implemented algorithm
Exceptions vs errors
• Error is a bug in the code that causes irregular output or stops program from executing.
• Exception is an irregular situation occurring during program execution on which
programmer has no control. This program is syntactically and logically correct
example: Divide by zero; try to open a file not exist.
When an exception occurs it should be handled by exception handling mechanism, in the
program
• Example ATM:
Entering wrong pin number-error
Page 1 of 4
Not much amount in account-exception
Exception handling in python
try:
#write a code that may generate exception
except:
#write a code how to handle the exception
Example:
try:
print(“result of 10/5’”,10/5)
print(“result of 10/0=“10/0)
except:
print(“divide by zero exception”)
types of exceptions
• EOFerror
• I0Error
• NameError
• ValueError
• TypeError
• KeyError
• ZeroDivisionError
• ImportError
• IndexError
• OverflowError
Testing:
It is the process of verifying the output of the program with sample datas.
How to debug a program?
• Carefully spot the origin of the error
• Print variable’s intermediate values
• Code tracing and stepping
Carefully spot the origin of the error
• Python interpreter will list the error if running program has error
• When python gives line number of an error:
• the error may be in this line number or the lines before the line number
1. a=int(input("enter a number"))
2. j="a"
3. for i in range(4):
4. print(j**i)
Traceback (most recent call last):
File "C:/Users/nadheen/Desktop/[Link]", line 4, in <module>
print(j**i)
Print variables intermediate values
• Depending upon algorithm the variable’s value changes .
• Sometime we get incorrect output but we can’t find it.

Page 2 of 4
• In this case add many print statement (temporarily)to inspect values of variables after
each step.
Code tracing
• Code tracing means execute the code one line at a time and watching its impact on
variables
Using debugger tool
• Is a specialized computer program/software that can be used to test and debug programs
written in a specific programming language.
• A debugger can be a separate program or integrated with(IDE)
• Python provides a separate debugger program called pdb.
Break points
• Break points are temporary halt markers in a source code that stop the debugger at the
marked code line
• Once break point is set up ,debugger executes till the line with breakpoint and then halts
pdb-python debugger
• Add first line of code as
import pdb
• Add the following line just above the code line from where you want to start tracing of
code
pdb.set_trace()
• Once you have done this and run the program python debugger will run the code and
show the following prompt in console window
ipdb>
b 45 set breakpoint on line 45
b list current breakpoints
cl clear all breakpoints
FAQs

 What is debugging?
 What is testing ?
 Difference between error and exception
 What is an exception?how it is handled?
 What are the types of exceptions?
 Difference between run-time and compile time error.
 What is syntax and semantic errors?
 What are logical errors?
 How to debug a program?
 What is pdb?
 What is a breakpoint?
 What is code tracing?
 Explain different types of errors in python with examples.
 Write the differences between testing and debugging.
 What is the difference between syntax error and semantics error?

Page 3 of 4
Page 4 of 4

You might also like