File I/O and Exception Handling Flashcards
7 cards from real HACKERRANK practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 File I/O and Exception Handling flashcards as text
Which Python keyword starts a block that catches exceptions?
Answer: except
Python uses `except` (paired with `try`) to catch and handle exceptions, unlike Java/C++ which use `catch`.
Which clause in a try/except construct always executes regardless of whether an exception occurred?
Answer: finally
The `finally` clause executes unconditionally after the try and any except blocks, making it ideal for cleanup code.
How do you re-raise the currently-handled exception inside an except block without losing the traceback?
Answer: raise
A bare `raise` statement inside an except block re-raises the current exception preserving the original traceback.
What is the base class for all built-in, non-system-exiting exceptions in Python?
Answer: Exception
`Exception` is the base class for all built-in exceptions that programs are expected to catch; `BaseException` also includes SystemExit and KeyboardInterrupt.
Which except clause syntax lets you capture the exception instance as a variable named 'e'?
Answer: except Exception as e:
The `except ExceptionType as variable:` syntax binds the caught exception instance to the given variable name.
Which block runs only if no exception was raised in the try block?
Answer: else
The `else` clause after an except block executes only when the try block completed without raising any exception.
What exception is raised when code tries to divide an integer by zero?
Answer: ZeroDivisionError
`ZeroDivisionError` is raised for division or modulo operations where the divisor is zero.