How to debug a python program - Debugging Programs
DEBUGGING
Debugging refers to the process of locating the place of error, cause of error, and correcting the code accordingly.
ERRORS
An error, sometimes called ‘a bug’, is anything in the code that prevents a program form compiling and running correctly.1. Compile Time Errors
Syntax errors
Syntax errors occur when rules of a programming language are misused i.e, when a grammatical rule of python is violated.
For example:X < - Y * Z
X = Y * Z
Semantics errors
Semantics errors occur when statements are not meaningful.
For example:
X*Y = Z, will result in a semantically error as an expression cannot come on the left side of an assignment statement.
2. Run time errors
Errors that occur during the execution of a program are run time errors. These are harder to detect errors. Some run time errors stop the execution of the program which is then called program “crashed” or “abnormally terminated”.
For example:
An infinite loop or wrong value.
3. Logical errors
Sometimes, even if u don’t encounter any error during the compile time and run time, your program doesn’t provide the correct result. This is because of the programmer’s mistaken analysis of the problem he or she is trying to solve. Such errors are logical errors.
For example:
An incorrectly implemented algorithm.
Use of variable before its initialization.
EXCEPTIONS
An exception refers to any irregular situation occurring during execution/run-time on which programmer has no control.
Exception handling in python – Basic idea
Exception handling in python involves the use of try and except clauses in a format wherein the code that may generate an exception is written in the try block and the code for handling exception is written in the except block.
try:
# write here a code that may generate an exception
except:
# write code here about what to do the exception has occurred
For example:# write here a code that may generate an exception
except:
# write code here about what to do the exception has occurred
try:
Print (“result of 10/5 = “, (10/5))
Print (“result of 10/0 = “, (10/0))
except:
Print (“divide by zero error! Denominator must not be zero! “)
Print (“result of 10/5 = “, (10/5))
Print (“result of 10/0 = “, (10/0))
except:
Print (“divide by zero error! Denominator must not be zero! “)
Exception Name | Description |
---|---|
EOFError | Raised when one of the built-in functions (input( )) hits an end-of-file condition (EOF) without reading any data. |
IOError | Raised when an I/O operation (such as print( ), the built-in open( ) function or a method of a file object) fails for an I/O-related, e.g., 'file not found' or 'disk full' |
NameError | Raised when an identifier name is not found |
IndexError | Raised when a sequence subscript or index is out of range, e.g., from a string of length 4 if you try to read a value of index of 4 or more i.e., string[4], string[5], string[-5], etc. will raise exception as legal indexes for a string of length 4 are 0, 1, 2, 3 and -1, -2, -3, -4 only. |
ImportError | Raised when an import statement fails to find the module definition or when a from … import fails to find a name that is to be imported. |
TypeError | Raised when an operation or function is applied to an object of inappropriate type, e.g., if you try to compute a square-root of a string value. |
ValueError | Raised when a built-in operation or function receives an argument within inappropriate value e.g., int("z10") will raise ValueError. |
ZeroDivisionError | Raised when the second argument of a division or modulo operation is zero. |
OverflowError | Raised when the result of an arithmetic operation is too large to be represented. |
KeyError | Raised when a mapping (dictionary) key is not found in the set of existing keys. |
DEBUGGING A PROGRAM
• An error can 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:
- Carefully spot the origin of error.
- Print variables’ intermediate values.
- Code tracing and stepping.
DEBUGGER TOOL
A debugging tool or debugger is a specialized computer program/software that can be used to test and debug programs written in a specific programming language.1. Working with integrated debugger tool of Spyder IDE
2. Working with python debugger - pdb
No comments: