Which statement correctly distinguishes a Python exception from a syntax error?
8 Exceptions and Error Handling Online Quiz Questions
Use this free practice quiz with 20 questions to review 8 Exceptions and Error Handling, test your knowledge, and prepare for your next test or exam.
True or false: In Python, an else block attached to a try statement runs only if the try block completes without raising an exception.
- A
True
- B
False
Which built-in exception class is raised by int("hello")?
Complete the standard Python exception-handling structure: place the risky operation in a block and handle a matching exception in an clause.
Which use best matches the purpose of a finally block in Python?
- A
Code that should run only after the
tryblock succeeds - B
Cleanup such as closing a resource or releasing a lock
- C
A replacement for every specific
exceptclause - D
Code that prevents the
tryblock from executing
Select all practices that support robust exception handling.
- A
Handle the most specific expected exception types.
- B
Use a bare
exceptfor every operation so no error can escape. - C
Catch an exception only when the function can take a useful action.
- D
Put unrelated application logic inside the
tryblock to reduce code length.
Inside an except block, a bare raise re-raises the current exception while preserving its traceback.
- A
True
- B
False
What single Python keyword, used without an exception argument, re-raises the current exception from inside an except block while preserving its traceback?
User-defined application exceptions should normally inherit from , not directly from .
A function catches an OSError while reading configuration and wants to expose a RuntimeError while preserving the original cause. Which code pattern does this?
- A
raise RuntimeError(...)with no reference to the original error - B
raise RuntimeError(...) from error - C
return RuntimeError(...)from the handler - D
finally RuntimeError(...)after the file operation
Select all choices that follow the material's guidance for preserving state and giving useful error messages in robust programs.
- A
Validate incoming data at a program boundary.
- B
Keep the
tryblock limited to the operation expected to fail. - C
Include passwords and other sensitive details in error messages for debugging.
- D
Give users a safe message that explains what failed and, when possible, how to fix it.
Explain how the read_first_lines design in the material demonstrates robust exception handling. Include how it validates input, limits the risky operation, distinguishes expected failures, preserves diagnostic information, and separates successful processing from error handling.
Which design best follows the guidance to keep risky operations narrow when converting user text to an integer and then using the result?
- A
try: number = int(text)followed by anexcept ValueErrorfor the conversion - B
Putting conversion, multiplication, printing, and unrelated logic in one broad
tryblock - C
Catching every exception and printing only
Something went wrong - D
Using a bare
exceptaround the entire function
Which situation is a runtime exception rather than a syntax error?
- A
A missing colon in an
ifstatement - B
Dividing by zero in syntactically valid code
- C
A misspelled keyword that prevents parsing
- D
An unterminated string literal
A program executes value = int("abc"). Which exception should a specific handler catch for this failure?
- A
TypeError - B
IndexError - C
ValueError - D
NameError
A function opens a resource and must release it regardless of whether the operation succeeds. Which clause is specifically intended for this cleanup?
- A
Only when the
tryblock raises an exception - B
Only when no
exceptclause matches - C
Only after the
elseblock runs - D
Whether or not an exception occurs
A withdraw function detects that the requested amount exceeds the balance and cannot produce a valid balance. What is the most appropriate way for the function to report this condition?
- A
Return
Nonefor every invalid input - B
Raise an exception that the caller can handle
- C
Use a bare
exceptinside the function - D
Print an error and return the original input
True or false: Catching every exception with a broad handler is generally a good way to make a program robust because it prevents unexpected failures from propagating.
- A
True
- B
False
Inside an except block, which Python statement re-raises the current exception while preserving its traceback?
What Python syntax should a function use when translating a low-level exception into a higher-level exception while explicitly preserving the original cause?