What exception is raised by int("abc")?
7 Exceptions and Error Handling Online Quiz Questions
Use this free practice quiz with 20 questions to review 7 Exceptions and Error Handling, test your knowledge, and prepare for your next test or exam.
In a Python try statement, when does the else clause execute?
- A
It runs before the
tryblock. - B
It runs whenever an exception is raised.
- C
It runs only when the
tryblock succeeds. - D
It always runs after
finally.
What is the primary purpose of raise ValueError("...") from error inside an except block?
- A
It preserves the original exception as the cause of the new exception.
- B
It suppresses all traceback information from the original exception.
- C
It retries the failed operation automatically.
- D
It converts every exception into
BaseException.
Which two practices follow the material's guidance for designing exception handlers? Select all correct choices.
- A
Handle specific exception types whenever possible.
- B
Place more specific handlers before more general handlers.
- C
Use a bare
except:for every recoverable error. - D
Put all application logic inside one large
tryblock.
Which two actions are consistent with the material's recommended validation process? Select all correct choices.
- A
Define which values the contract accepts.
- B
Allow invalid data to continue until a later operation fails.
- C
Validate input where it enters a function or component.
- D
Avoid stating the violated requirement to the caller.
True or false: A finally block executes even if the try block returns before reaching its end.
- A
True
- B
False
True or false: A bare except: is generally recommended for ordinary application logic because it handles only common runtime errors.
- A
True
- B
False
In the documented set_percentage(value) example, which Python exception class should be raised when value is less than 0 or greater than 100?
When reading a Python traceback from the bottom upward, what two pieces of information does the final line identify?
Complete both statements with the appropriate Python exception class. Raise when an input has an inappropriate type; raise when an input has an acceptable general type but an invalid value.
Complete the statement with the recommended keyword: open("notes.txt", encoding="utf-8") as file:
Explain the three questions an effective error message should answer, and give a concise example that follows those principles.
A try statement needs separate responses for FileNotFoundError and other Exception instances. Which handler order is correct?
- A
except Exception:beforeexcept FileNotFoundError: - B
except FileNotFoundError:beforeexcept Exception: - C
except:before every typed handler - D
The order does not affect which handler runs
A program converts user input inside a try block. The conversion succeeds, and the program should run code that depends on that success without placing that code inside the protected operation. Which clause should contain that code?
- A
except
- B
else
- C
finally
- D
raise
A program attempts to open settings.txt, but that file does not exist. Which specific exception should normally be handled to report that the settings file was not found?
- A
FileNotFoundError
- B
PermissionError
- C
ValueError
- D
KeyError
Which exception type is raised by the expression "total" + 3 because the operation receives an inappropriate type?
- A
ValueError
- B
NameError
- C
TypeError
- D
AssertionError
A function requires its username argument to be a string. If the caller passes an integer, which exception should the function raise according to the validation guidance?
- A
ValueError
- B
TypeError
- C
IndexError
- D
OSError
True or false: If a try block executes a return statement, its finally block still executes before the function returns.
- A
True
- B
False
In the example create_user(username, age), what exception should be raised when username is a string containing only whitespace?
Which Python keyword begins the resource-management construct that automatically performs cleanup for a file when an exception occurs inside the block?