The shape
try:
return int(text)
except ValueError:
return None
If int() raises ValueError, control jumps to the handler. If it raises anything else, the handler is skipped and the error keeps travelling up. That selectivity is the point.
Catch what you expect, nothing more
except: # catches everything
This is almost always a mistake. It catches the error you were thinking of, and also your typos, your NameErrors, your AttributeErrors from a refactor you half-finished. The program stops crashing and starts producing wrong answers quietly, which is strictly worse: a crash tells you where to look.
The second program on this page has a deliberate typo inside a bare except. It returns 0, cheerfully, and nothing anywhere says a name was misspelled.
Name the exception:
except ValueError:
except (KeyError, IndexError):
except Exception as e: # broad, but at least not BaseException
as e, and why you want it
except ValueError as e:
print("could not convert:", e)
The exception object carries the detail — which key was missing, which value would not parse. Discarding it and printing "something went wrong" throws away the only part that would have helped.
else and finally
else runs when the try block raised nothing. It keeps the risky line alone in the try, so the handler cannot accidentally catch an error from the follow-up code.finally runs either way, raised or not. It is where cleanup goes.
For files and locks, prefer with, which does the same job with less ceremony.
Raising your own
Handling is half of it. When a caller hands you something impossible, say so:
if age < 0:
raise ValueError(f"age cannot be negative, got {age}")
Include the offending value in the message. "Invalid input" costs the next person a debugging session; "got -1" ends it immediately.
Ask forgiveness, not permission
Python leans toward trying the operation and handling the failure, rather than checking first. Checking if key in d before d[key] does the lookup twice and still has a gap between the check and the use. Try it and catch KeyError; it is faster in the common case and correct in all of them.