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
Placing controls in code
Debugging code, Use of Python Debugger
Types of Errors: Syntax, Type, Run-Time, and Logical
Generally, three types of errors appear in a computer program:
Syntax Errors,
Logical Errors and
Runtime errors.
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
Incorrect assumptions: Making assumptions about the input data or the behavior of other parts of the program can lead to logical errors.
Incomplete understanding of the problem: Not fully understanding the problem you're trying to solve can result in logical errors that prevent your code from producing the correct output.
Incorrect use of algorithms or formulas: Applying algorithms or formulas incorrectly can cause your code to produce incorrect results.
Inadequate error handling: Not handling errors gracefully can lead to unexpected behavior and 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:
Test your code with different inputs: Try feeding your code a variety of input data to see if it produces the correct output for each case.
Use print statements to debug your code: Insert print statements at key points in your code to inspect variables, check for potential errors, and understand the program's flow.
Write unit tests: Unit tests can help identify logical errors early in the development process by ensuring that specific functions or modules behave as expected.
Use a debugger: Debuggers like the built-in pdb or integrated debugging tools in IDEs allow you to step through your code, inspect variables, and pinpoint the source of logical errors.
Troubleshooting logical errors
Once you've identified a potential logical error, use the following steps to troubleshoot:
Rewrite the faulty logic: Carefully examine the code snippet where the error is suspected and rewrite the logic to ensure it aligns with the correct solution.
Check for typos or other syntax errors: Sometimes, a seemingly correct logic can be derailed by a typo or a misplaced symbol. Double-check your code for any syntax errors.
Verify assumptions and inputs: Ensure that your code is making correct assumptions about the input data and that the input data is valid and represents the expected format.
Review algorithms and formulas: Validate that you're using the correct algorithms or formulas and that you're applying them correctly.
Test edge cases and unexpected inputs: Consider inputs that might not be immediately apparent but could potentially cause the logic to break.
Seek help from others: If you're stuck, don't hesitate to seek help from experienced programmers or online communities to get a fresh perspective on the problem.
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
The try-except Block
The try-finally Block
Nested try Block
Built-in Exceptions
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):
This simple yet powerful tool comes bundled with Python.
Use import pdb; pdb.set_trace() to set breakpoints and step through your code line by line.
Explore variables, call stacks, and even run custom commands within the debugger.
Great for understanding how your code executes and pinpointing issues.
Logging:
Print statements are your basic debugging tool, but they can get messy and hard to manage.
Use the logging module to create structured logs with levels (e.g., INFO, DEBUG, ERROR).
Configure handlers to output logs to files, console, or even email for remote monitoring.
Helps track program flow, identify suspicious behavior, and diagnose errors in complex code.
Assertions:
Embed checks within your code to verify assumptions and expected states.
Use assert statements followed by conditions that should hold true.
If the condition fails, the program throws an AssertionError, pinpointing the problematic line.
Useful for catching unexpected situations and ensuring specific code sections behave as intended.
Interactive debuggers:
Tools like PyCharm, VS Code, and Spyder offer integrated debuggers with advanced features.
Set breakpoints, inspect variables, visualize data structures, and even debug multithreaded applications.
Often provide visual representations of your code flow and allow you to manipulate variables on the fly.
Great for complex debugging tasks and gaining deeper insights into your program's internals.