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

Chapter 6 Engdebugging Programs

The document discusses the process of debugging programs, particularly in Python, highlighting the types of errors (compile time, run time, and logical errors) and exceptions that can occur during program execution. It explains debugging techniques such as identifying error locations, printing variable values, and code tracing, along with the use of try and except clauses for exception handling. Additionally, it lists common exceptions in Python and emphasizes the importance of testing to ensure program correctness.

Uploaded by

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

Chapter 6 Engdebugging Programs

The document discusses the process of debugging programs, particularly in Python, highlighting the types of errors (compile time, run time, and logical errors) and exceptions that can occur during program execution. It explains debugging techniques such as identifying error locations, printing variable values, and code tracing, along with the use of try and except clauses for exception handling. Additionally, it lists common exceptions in Python and emphasizes the importance of testing to ensure program correctness.

Uploaded by

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

Debugging Programs

Based on CBSE Curriculum


Class -11

[Link]
Introduction
• When we develop a program for some task, generation
of errors is so obvious.

• A Programmer needs to find these errors and try to fix


them until the program become error free. This process
is called debugging.

• If you are familiar with an error or exception, it will be


easier for you to debug the program.

• In this chapter, we will discuss python errors, exception,


debugging [Link]
and python debugger pdb.
What is Debugging?
• Debugging means the process of finding errors, finding
reasons of their occurrence and techniques of their
fixation.

ERRORS and EXCEPTION


• Errors and Exception, both interrupts the Program
Execution.
• Errors and Exception are not same.
• Errors, usually finds out at the time of translation.
• Whereas, Exception generates during program run, due to an
input.

[Link]
Error
• An error, also known as a bug, is a programming code
that prevents a program from its successful
interpretation.

• Some errors are less harmful in nature whereas some


are very harmful. Some errors are very difficult to find.

• Errors are of three types –


• Compile Time Error

• Run Time Error

• Logical Error
[Link]
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-

x=(x*y)

• 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
[Link]
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. This error generates
because of wrong analysis by programmer.

For ex-
• To use a variable without an initial value.

• To provide wrong parameters to a function.

These errors are most difficult to find out and handle.

[Link]
Run Time Error
• These errors generates during a program execution and
known as run time errors.
• These errors are difficult to find out.
• Some run time errors prevents a program from execution
which is known as program crash or abnormal termination of
program.
• Most run time errors like infinite loop or wrong value are easy
to detect.
• Python is having provision of checkpoints to handle these
errors.
• It is important to prevent a program from crashing in case of
run time error. A program should be robust.

[Link]
Exception
• Exception is a state when a program terminates abnormally
and it can’t be controlled.
For ex-
• a=10
• b=0
• c=a/b (Mathematical Exception)
Another Ex-
– Inputting wrong account number in an ATM is
an error.
– But receiving less amount than expected is an
Exception.

An exception cause termination of a program.


[Link]
Exception Handling in Python
• In Python, try and except clauses are used to handle an
exception. The block which has the probability of occurring an
exception, that block is to be written under try clause and the
code to handle the exception is to be written under except
clause. Syntax is as under-
try:
# code with probability of exception will be written here.
except:
#code to handle exception will be written here.

OUTPUT

[Link]
Available Exceptions in Python

Exception Name Description


IOError This exception generates due to problem in input or output.
NameError This exception generates due to unavailability of an identifier.
IndexError This exception generates when subscript of a sequence is out of range.
ImportError This exception generates due to failing of import statement.
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.

[Link]
How to debug a program?
• An error can also be debugged by correcting the code.

• Compile time error can be debugged before program run.

• Logical error can be solved by Testing.

• Testing is to be used with some sample values to check the


correct behavior of the program.

• There are following techniques of debugging-


– To identify the place of Error generation.

– By printing step wise value of Variable.

– By tracing the Program Code.

[Link]
Detection of origin of an Error
• Look at following code carefully-

• Following error generates during its translation-

• We need to change the statement to remove this error


OUTPUT

[Link]
Printing of stepwise values of Variable
• See following code carefully - • Following wrong output generated on its run.

• We need to print value of each variable


at every step to detect the error.

OUTPUT We can find out


the problem by
watching these
values and then
can change the
code accordingly.

[Link]
Printing of stepwise values of Variable
• Now we can change the code to get the corrected result.
a=b OUTPUT
change
b=c

corrected
code

Therefore, we can say that debugging means to find the


error and to correct it until program run successfully.

[Link]
Tracing of code
• Another technique for removing an error is code
tracing. In this technique, lines are to be executed
one by one and their effect on variables is to be
observed. Debugging tool or debugger tool is
provided in Python for this.
• In Python3.6.5, to make debugger tool available, click
on debugger option in debug menu.

[Link]
Tracing of code
• Then, a box will be opened and a message will come
saying DEBUG ON

• Then, we will open our program from file menu and


will run it.
[Link]
Tracing of code
• Then, program environment will be shown like this in
debugger.

• Now, click on STEP button. All statements will be executed one by one and result
will be display in output. When we will get wrong value, we can stop the program
there and can correct the code. This process will be continued until program results
in correct desired output.
[Link]
Thank you
Please follow us on our blog-
[Link]

[Link]

You might also like