Exceptions & Files · Question 10 · Medium
A custom exception carries a message built inside withdraw. What does printing the caught exception show?
class LowBalance(Exception):
pass
def withdraw(balance, amount):
if amount > balance:
raise LowBalance("short by " + str(amount - balance))
return balance - amount
try:
withdraw(500, 800)
except LowBalance as e:
print(e)
- LowBalance
- LowBalance: short by 300
- short by 300
- -300
Answer and explanation
Answer: C. short by 300
Explanation: 800 is more than 500, so LowBalance is raised with the message short by 300. Printing an exception object shows just the message passed to it, without the class name.
Previous: An out-of-range list index is handled by two except clauses, the first naming… · Next: Lines of numbers are read from a file-like object inside a with block and…
Practise exceptions & files
Try a fresh set of exceptions & files questions with an explanation after every answer, free and without signing up: Exceptions & Files practice. Or sit a full placement mock test.
More exceptions & files questions
- io.StringIO lets a string be read like a text file. How many lines does…
- A try statement has except, else and finally clauses, and an error occurs in…
- An out-of-range list index is handled by two except clauses, the first naming…
- Lines of numbers are read from a file-like object inside a with block and…
- read() is called three times on the same file-like object, first with a size…
- Why is with open("data.txt") as f: preferred over calling open() and close()…
- The only except clause below names TypeError, but the conversion fails in a…
- readline() is called twice and then read() once on a three-line file-like…
- A function returns from inside try, and its finally block has a print. In what…
- All three parts of this try statement contain a return. What does risky() give…
More python practice
Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge