PCEP Error Handling and Debugging 1 — Questions and Answers
Question 1: What does the "try...except" block in Python do?
- Executes code only if no errors occur
- Catches and handles exceptions or errors that occur during execution (Correct answer)
- Terminates the program if an error occurs
- Ignores all errors during execution
Correct answer: Catches and handles exceptions or errors that occur during execution
The `try...except` block in Python is a mechanism for handling runtime errors, known as exceptions, gracefully. Code within the `try` block is executed, and if an exception occurs, execution immediately jumps to the `except` block. This allows the program to handle the error without crashing, ensuring more robust and stable execution.
Question 2: What type of error occurs if you try to access an undefined variable?
- TypeError
- IndexError
- NameError (Correct answer)
- ValueError
Correct answer: NameError
A `NameError` occurs in Python when you try to use a variable or function name that has not been defined or is not accessible in the current scope. The interpreter cannot find a reference for the name you are trying to access, leading to this specific type of error. It indicates a problem with how a name is referenced in the code.
Question 3: What is the purpose of the "else" block in a "try...except" structure in Python?
- To handle any exceptions raised in the try block.
- To execute code if the try block raises an exception.
- To execute code if no exceptions are raised in the try block. (Correct answer)
- To execute code before the try block.
Correct answer: To execute code if no exceptions are raised in the try block.
In a `try...except...else` structure, the `else` block is executed only if the code within the `try` block runs completely without raising any exceptions. It provides a way to run code that depends on the `try` block's success, separate from the `try` block itself. This helps in organizing code that should only execute when no errors occur.
Question 4: What does the "finally" block do in "a try...except" structure?
- It executes only if an exception is raised.
- It executes only if no exception is raised.
- It executes whether an exception is raised or not. (Correct answer)
- It is optional and not used in exception handling.
Correct answer: It executes whether an exception is raised or not.
The `finally` block in a `try...except...finally` structure is guaranteed to execute regardless of whether an exception occurred in the `try` block or was handled by an `except` block. It is typically used for cleanup operations, such as closing files or releasing resources. This ensures that essential finalization steps are always performed, preventing resource leaks.
Question 5: Which of the following statements is true about debugging in Python?
- You can only use print statements for debugging in Python.
- The "pdb" module is Python’s built-in debugger that allows you to step through code, set breakpoints, and inspect variables. (Correct answer)
- Python does not have any debugging tools; you need to use external editors.
- Debugging is only necessary for large projects; small scripts do not need it.
Correct answer: The "pdb" module is Python’s built-in debugger that allows you to step through code, set breakpoints, and inspect variables.
Python includes a powerful built-in debugger called `pdb` (Python Debugger). This module allows developers to interactively step through their code line by line, set breakpoints to pause execution at specific points, and inspect the values of variables. This functionality is crucial for identifying, understanding, and fixing bugs in Python programs efficiently.
What does the "try...except" block in Python do?