Unit 1 - Debugging

 CBSE Revision Notes

Class-11 Computer Science (New Syllabus)
Unit 1: Programming and Computational Thinking (PCT-1)


Debugging

Idea of debugging:
Debugging actually means when an error occurs in your program how you are going to find the error and correct that error.
Errors and Exceptions: In python there are two types of errors:

  1. syntax error
  2. exceptions.

Syntax error: Syntax error basically means if there is something wrong we have written in our program that doesn’t exist it includes spelling mistakes or if something is missing from the syntax.. E.g. we write prit() in place of print() it is a syntax error. Our interpreter finds these errors before executing the program and point out towards the syntax errors. Let’s take another example to understand syntax error

while True
print(“Hello”)

It will give a syntax error because “:” is missing after while statement.

Exceptions: The other kind of error in python is exception. Even if a statement or expression is syntactically correct, it may cause an error when an attempt is made to execute it. Errors detected during execution are called exceptions. Exceptions come in different types, and the type is printed as part of the message. The types in the example are ZeroDivisionError, NameError, TypeError. Let’s see an example

>>10/0 it’s an exception ZeroDivisionError

Handling the exception: If you have some suspicious code that may raise an exception, you can defend your program by placing the suspicious code in a try: block. After the try: block, include an except: statement, followed by a block of code which handles the problem as elegantly as possible. Now let’s see its syntax.

try:

Do your operation here.

except ExceptionOne:

If there is ExceptionOne, then execute this block.

except ExceptionTwo:

If there is ExceptionTwo, then execute this block.

else:

If there is no exception then execute this block.

Here are few important points about the above-mentioned syntax-

  1. A single try statement can have multiple except statements. This is useful when the try block contains statements that may throw different types of exceptions.
  2. You can also provide a generic except clause, which handles any exception.
  3. After the except clause(s), you can include an else-clause.The code in the else block executes if the code in the try: block does not raise an exception. It is optional block.
  4. The else-block is a good place for code that does not need the try: block's protection.

Debugging: Debugging means finding the errors and removing it. If we don’t receive expected results it is possible that we may have done something wrong. To check where it has gone wrong we use breakpoints. Breakpoint actually stops the program at that point and shows the values of variables at that particular point. Breakpoint is quiet useful tool for debugging the program and to check where it’s going wrong.

The module pdb defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame. It also supports post-mortem debugging and can be called under program control.