You call float("abc"). What does Python do?
Crashes? It can't turn "abc" into a number.
It raises a ValueError. If you don't handle that error, the whole program stops. Real expense files have rows like "N/A" and "TBD" where amounts should be — one corrupted row shouldn't take down every downstream function.
So how do I "handle" the error — is there a way to try the parse and fall back if it fails?
That's exactly what try/except is. You attempt the risky operation in the try block; if it raises, the except block runs instead:
try:
return float(value)
except ValueError:
return NoneIf float(value) succeeds, the function returns the float. If it raises ValueError, the except block returns None and the program keeps running.
What if someone passes in None instead of a string? Does that raise ValueError too?
No — float(None) raises TypeError. Different exception, same spirit. You can catch both in one except by listing them as a tuple:
try:
return float(value)
except (ValueError, TypeError):
return NoneSo the function has two contracts — "here's a float" on success, and "here's None, the parse failed" on bad input. The caller decides what to do with None.
That's exactly the shape. A parse that might fail returns an optional value, and the caller handles the failure case. This is the try/except pattern you'll reach for every time you touch data that might not be well-formed.
try / exceptTL;DR: attempt a risky operation; handle the error without crashing.
try: — code that might raiseexcept Exception: — runs only when an exception firesexcept (A, B): — catches any of a tuple of exception types| Call | Raises |
|---|---|
float("abc") | ValueError |
float(None) | TypeError |
int("1.5") | ValueError |
Return a safe sentinel like None so callers can detect and handle the failure explicitly.
Write `safe_parse_amount(value)` that returns `float(value)` on success or `None` on a parse failure (ValueError or TypeError).
Tap each step for scaffolded hints.
No blank-editor panic.
You call float("abc"). What does Python do?
Crashes? It can't turn "abc" into a number.
It raises a ValueError. If you don't handle that error, the whole program stops. Real expense files have rows like "N/A" and "TBD" where amounts should be — one corrupted row shouldn't take down every downstream function.
So how do I "handle" the error — is there a way to try the parse and fall back if it fails?
That's exactly what try/except is. You attempt the risky operation in the try block; if it raises, the except block runs instead:
try:
return float(value)
except ValueError:
return NoneIf float(value) succeeds, the function returns the float. If it raises ValueError, the except block returns None and the program keeps running.
What if someone passes in None instead of a string? Does that raise ValueError too?
No — float(None) raises TypeError. Different exception, same spirit. You can catch both in one except by listing them as a tuple:
try:
return float(value)
except (ValueError, TypeError):
return NoneSo the function has two contracts — "here's a float" on success, and "here's None, the parse failed" on bad input. The caller decides what to do with None.
That's exactly the shape. A parse that might fail returns an optional value, and the caller handles the failure case. This is the try/except pattern you'll reach for every time you touch data that might not be well-formed.
try / exceptTL;DR: attempt a risky operation; handle the error without crashing.
try: — code that might raiseexcept Exception: — runs only when an exception firesexcept (A, B): — catches any of a tuple of exception types| Call | Raises |
|---|---|
float("abc") | ValueError |
float(None) | TypeError |
int("1.5") | ValueError |
Return a safe sentinel like None so callers can detect and handle the failure explicitly.