Exception silencing suppresses an exception without informing the user or exposing useful diagnostic information. Catching every exception, or even an unnecessarily broad class, can hide failures and make debugging frustrating. section 5.2.4
A maintainable alternative is to let exceptions travel upward to a context that can respond meaningfully. Handle specific exceptions, explain the problem, guide the user, and retain diagnostic information rather than silently continuing. section 5.2.4
Retry invalid division input
A division program leaves its worker functions unchanged and handles ZeroDivisionError and ValueError around the higher-level call. It reports the cause, asks for corrected input, and retries until the operation succeeds. section 5.2.4
try:
# some code that might raise an exception
except:
pass
try:
# some code that might raise an exception
except Exception:
pass
Compare the empty handlers that silence every exception with the narrower, still-problematic Exception handler. section 5.2.4
def func_a():
dividend = float(input("Enter a dividend value: "))
divisor = float(input("Enter a divisor value: "))
result = func_b(dividend, divisor)
print(f"dividing {dividend} by {divisor} = {result}")
def func_b(dividend: float, divisor: float) -> float:
return func_c(dividend, divisor)
def func_c(dividend: float, divisor: float) -> float:
return dividend / divisor
successful = False
while not successful:
try:
func_a()
except ZeroDivisionError as e:
print(f"The divisor can't be a zero value, error:", e)
except ValueError as e:
print(
f"The dividend and divisor must be a string that represents a
number, error:",
e,
)
else:
successful = True
finally:
if successful:
print("Thanks for running the program")
else:
print("Try entering a dividend and divisor again")
Follow the improved pattern: propagate failures through helper functions, handle specific errors at a higher level, and retry with user feedback. section 5.2.4
