Notes Chapter 6 Debugging Programs
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 techniques 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.
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
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
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.
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.
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.
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-
Available Exceptions in Python
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.
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
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.
Printing of stepwise values of Variable
Now we can change the code to get the corrected result.
Therefore, we can say that debugging means to find the error and to correct it until program run successfully.
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.
• 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
• 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.