Error Handling and Debugging

Objective : What I want you to learn?

ABC

Rationale : Why this is useful to you?

ABC

Learning Outcomes : What I want you to do after completing this topic?

ABC

Contents

Types of Errors: Syntax, Type, Run-Time, and Logical

Generally, three types of errors appear in a computer program: 

Syntax errors are the most common type of errors one faces while writing a program, whether you are new to programming or an experienced programmer. Syntax errors are related to the rules of grammar of a certain language. Syntax errors occur whenever the rules laid down by the language are not followed. In Python, there are well-defined rules for giving name to an identifier, that is, a variable, a function, a class, a module or any Python object. Similarly, Python keywords should be used as per the syntax defined. Whenever these rules are not followed, Python interpreter displays a syntax error message. 

A simple example of declaring a variable in Python interactive shell is given below :

>>> name="Python

   File "<stdin>", line 1

      name="Python

           ^

SyntaxError: unterminated string literal (detected at line 1)

Python interpreter displays syntax error along with a certain explanatory message. In the above example, because the quotation symbol is not closed, the Syntax error occurs. 

Similarly, Python requires each function name should be followed by parantheses inside which the function arguments should be given.

In the following example, we get a syntax error −

>>> print "Hello"

   File "<stdin>", line 1

      print "Hello"

      ^^^^^^^^^^^^^

SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

The reason can be understood from the error message, that the print() function is missing parentheses. 

Syntax errors are easy to identify and rectify. The IDE such as VS Code makes it easy. However, sometimes, your code doesn't show any syntax errors, but still the output of the program is not what you anticipate. Such errors are logical errors. They are hard to detect, as the error lies in the logic used in the code. You learn by experience how to correct logical errors. 

Logical Errors:


Logical errors in Python, also known as semantic errors, are the most challenging type of error to identify and fix. These errors occur when your code runs without any syntax or runtime errors but produces incorrect results due to a flaw in the algorithm or the overall logic of your program.

Common causes of logical errors

Identifying logical errors

Logic errors can be tricky to identify, as they may not cause your program to crash or throw exceptions. Instead, they can manifest as unexpected or incorrect outputs. Here are some strategies for identifying logical errors in Python code:

Troubleshooting logical errors

Once you've identified a potential logical error, use the following steps to troubleshoot:

Remember, debugging logical errors requires patience, careful analysis, and a deep understanding of the problem you're trying to solve. With persistence and the right approach, you can effectively identify and fix these challenging errors in your Python code.

Runtime Errors:

 The third type of error is a runtime error also called an exception. There is no syntax error nor is there any logical error in your program. Most of the time, the program gives desired output. Still, in some specific situations, you get abnormal behaviour of the program, such as the program abnormally terminates or gives some absurd result. 

Here are a few common runtime errors in Python:

1. ZeroDivisionError: Occurs when you try to divide a number by zero.

2. IndexError: Occurs when you try to access an index in a list, tuple, or string that doesn't exist.

3. NameError: Occurs when you use a variable or function that hasn't been defined.

4. TypeError: Occurs when you perform an operation on a data type that doesn't support it, like adding a string to an integer.

5. FileNotFoundError: Occurs when you try to open or manipulate a file that doesn't exist.

Placing controls in code

Debugging code

Debugging code in Python can be a frustrating but essential part of the development process. Luckily, Python offers several tools and techniques to help you track down and squash those pesky bugs. Here's a rundown of some popular approaches: 

Built-in debugger (pdb):

Logging:

Assertions:

Interactive debuggers: