Lesson 8 of 8
Try / Except
Concept
When code might fail (network calls, file reads, bad data), wrap it in
try/except. The try block runs your code. If it crashes, Python jumps to the matching except block instead of killing your program.
m3shdup/app/intrinsic.py
def idle_minutes() -> int:
try:
val = os.environ.get("M3SHDUP_INTRINSIC_IDLE_MINUTES", "10")
return int(val)
except ValueError:
return 10
# If val = "10" → int("10") = 10 → returns 10
# If val = "abc" → int("abc") crashes → except catches it → returns 10
What's happening
This is a real pattern from your code. Someone could set the env var to garbage. Without try/except,
int('abc') would crash the whole app. With it, you get a safe fallback to 10. The ValueError after except says 'only catch this specific error type.'