Exceptions
Introduction
1. What are exceptions used for?
Exceptions are a way of handling errors and exceptional conditions that may occur during the execution of a program.
2. What happens when an exception is raised?
When an exception is raised, it triggers a propagation workflow to perform exception handling. If the current call does not handle the exception, it is propagated up to caller.
3. Explain the two main categories of exceptions (Compilation and Execution). When do they occur? Provide example(s) for both of them.
- Compilation Exceptions occurs when during the parsing and compilation of the Python Source Code.
- Example: Syntax Errors
- Execution Time Exceptions occurs during the runtime of the program.
- Example: ValueError, KeyError, etc
4. Explain the differences between Syntax Error and Execution-Time Exceptions.
- Syntax Errors occurs when the Python interpreter encounters a syntax error in the source code, which leads to the termination of the program.
- Execution-Time Exceptions are raised when the program is syntactically correct, but the code results in an error. This error does not stop the execution of the program, however, it changes the normal flow of the program.
5. Explain call stack.
The call stack is a data structure used by the computer’s memory to keep track of active function calls in a program.
6. Explain the order of the call stack. Is it First In First Out (FIFO) or Last in First Out (LIFO)?
- The call stack order is Last in First out (LIFO), meaning that the last function call made is the first one to be completed.
- The origin of the call is at the bottom of the stack and the latest call is at the top of the stack.
7. Explain how exception is being propagated if the exception was raised in function 3 (f3) that is called within f2 and f2 is called within f1. Assume that f3 and f2 do not handle the exception.
If the exception is not handled in f3, then it will propagate to f2. If unhandled in f2, it will propagate to f1.
8. Explain the use case of error handling in handling exceptions.
Exceptions are primarily used for error handling. When something unexpected or erroneous happens during the execution of a program, Python raises an exception. You can catch and handle these exceptions to prevent your program from terminating abruptly.
9. Explain the use case of debugging in handling exceptions.
Exceptions provide valuable information about the error that occurred, including a description of the problem, the location in the code where the error happened, and the type of error.
10. Explain the use case of control-flow in handling exceptions.
Exceptions can also be used for controlling the flow of your program. You can raise custom exceptions to signal specific conditions in your code and then catch and handle them accordingly.
Control Flow
Common Exceptions
1. Explain SyntaxError and provide example(s).
- Description: Occurs when the Python interpreter encounters a syntax error in the code, which leads to the termination of the program.
- Example: Missing colons, incorrect indentation, misspelled keywords.
2. Explain TypeError and provide example(s).
- Description: Raised when an operation or function is applied to an object of an inappropriate type.
- Example: Attempting to concatenate a string and an integer.
3. Explain NameError and provide example(s).
- Description: Raised when a local or global name is not found.
- Example: Trying to use a variable or a function that is not defined.
4. Explain ValueError and provide example(s).
- Description: Raised when a built-in operation or function receives an invalid argument or input.
- Example: Converting a string to an integer where the string does not represent a valid integer.
5. Explain ZeroDivisionError and provide example(s).
- Description: Raised when attempting to divide by zero.
- Example: Dividing by zero.
6. Explain KeyError and provide example(s).
- Description: Raised when trying to access a non-existent key in a dictionary.
- Example: Attempting to access a key that is not present in a dictionary.
7. Explain AttributeError and provide example(s).
- Description: Raised when an attribute reference or assignment fails (not found on an object).
- Example: Attempting to access a non-existent attribute of a class instance.
8. Explain ImportError and provide example(s).
- Description: Raised when the import statement fails to find or load a module.
- Example: Attempting to import a module that does not exist or not stored in the
sys.path.
9. Explain IndexError and provide example(s).
- Description: Raised when an index is out range for a sequence type (e.g. tuple, string, list).
- Example: Accessing an element at an index that is beyond the length of a list.
Raise
1. What is keyword raise used for?
The
raise keyword is used to raise an exception and terminates the program.
2. Explain the difference between raise and return.
raise is used to signal something unexpected or exceptional has occurred in the program (and terminates the program). Whereas, return is used to provide a result and potentially allow the program to continue its normal flow.
3. Use the raise keyword create an expression that will raise an error if the value (x) is below 0.
if x < 0:
raise ValueError("Sorry, no numbers below zero")