liveBook · Manning

Exception silencing

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

Exception silencing is suppressing an exception without reporting it or exposing information about its cause.

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

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Contrasts broad silent catches with specific handlers, propagation, retry control flow, and user feedback.
Examples
Uses division input, ZeroDivisionError, ValueError, KeyboardInterrupt, and a logger as concrete cases.
What you can do after
Diagnose/debug with targeted handlers, informative messages, upward propagation, and logging instead of silent failure.
How it connects
Connects exception handling with call stacks, retry loops, user input validation, and logging.

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.