The program below stops with an error. Which statement correctly describes the problem?
try:
age = int("twenty")
except TypeError:
age = 0
print(age)
A handler catches only its own class and subclasses. Use except ValueError.
Exception Handling · Exercise 3 of 4
Each Python program handles exceptions incorrectly: a syntax error in the try statement, a wrong exception type, an over-broad handler that hides a bug, or code that relies on a failed try. Choose the option that names the real problem.
The program below stops with an error. Which statement correctly describes the problem?
try:
age = int("twenty")
except TypeError:
age = 0
print(age)
A handler catches only its own class and subclasses. Use except ValueError.
Python refuses to run the program below. Which statement correctly describes the problem?
try:
result = 10 / 0
print("finished")
Python reports "expected 'except' or 'finally' block".
Python refuses to run the program below. Which statement correctly describes the problem?
try:
n = int("12")
else:
print("ok", n)
except ValueError:
print("bad")
Move the except clause above else.
The text "42" is a valid number, yet the program prints "Invalid number". Which statement correctly identifies the bug?
value = "42"
try:
number = int(valu)
except:
print("Invalid number")
else:
print("Number:", number)
A bare except turns programming mistakes into misleading messages.
A non-numeric quantity should print "Quantity must be a number". Which statement correctly identifies the bug?
qty_text = "ten"
try:
qty = int(qty_text)
except Exception:
print("Something went wrong")
except ValueError:
print("Quantity must be a number")
Order handlers from specific to general.
The program below stops with an error. Which statement correctly describes the problem?
def set_age(age):
if age < 0:
raise "Age cannot be negative"
return age
set_age(-3)
Python reports "exceptions must derive from BaseException".
The program below stops with an error. Which statement correctly describes the problem?
try:
port = int("80a")
except ValueErorr:
port = 8080
print(port)
The name in an except clause is looked up only when an exception reaches it, so the typo goes unnoticed until something fails.
A transfer to an unknown account should leave both balances unchanged. Which statement correctly identifies the bug?
accounts = {"asha": 1000, "ravi": 500}
def transfer(src, dst, amount):
try:
accounts[src] -= amount
accounts[dst] += amount
except KeyError:
print("Unknown account:", dst)
transfer("asha", "meera", 300)
print(accounts)
An exception stops the try block midway; work already done stays done. Check first, or perform the changes only when both keys exist.
The program prints "bad input" and then stops with an error. Which statement correctly describes the problem?
try:
value = int("abc")
except ValueError:
print("bad input")
print(value * 2)
Set a fallback in the handler (value = 0) or return/stop there.
The program below stops with an error. Which statement correctly describes the problem?
def check(stock):
if stock < 0:
raise
return stock
check(-1)
Python reports "No active exception to reraise".
Timed online tests with instant scores and explanations.