u05-slides-exceptions-debugging-flashcards
What is an exception in Python?
An object created when an error occurs that carries information about what went wrong
How do you raise an exception in Python?
Using the raise statement (e.g., raise ValueError(“message”))
What are the main blocks in exception handling?
try (where code might raise exception), except (handles exceptions), finally (always executes), and else (executes if no exception)
What are some common predefined exceptions in Python?
TypeError, ValueError, IndexError, KeyError, ZeroDivisionError, FileNotFoundError, ModuleNotFoundError
How are exceptions passed in Python?
Upwards through the calling hierarchy until caught or program fails
What happens when an exception is raised?
Program execution jumps to where the exception is caught or to the end of the program
How do you catch multiple exceptions at once?
Using parentheses in except statement (e.g., except (ValueError, TypeError, IndexError))
What’s the purpose of the ‘as’ keyword in except blocks?
It allows you to access the exception object and its details
What’s the purpose of the finally block?
To execute code regardless of whether an exception occurred or not
What’s the purpose of the else block in exception handling?
To execute code only if no exception occurred in the try block
Why is the order important in catching multiple exceptions?
The first matching except clause is executed so more specific exceptions should come before general ones
Can exception handling be nested?
Yes you can have try statements within except else and finally clauses
What is debugging?
The process of identifying and removing errors from code during development
What is a breakpoint?
A marker that pauses program execution at a specific line of code
How do you set a breakpoint in VS Code?
By clicking in the gutter to the left of the line numbers