Try and Except
๐ What is Try/Except? Pythia says: 'Even the best code faces unexpected situations - a file that does not exist, a user who types letters instead of numbers. try/except is your safety net!'

Appy Saysโฆ
What happens when your app crashes because a user typed a word where a number was expected? Without try/except, your whole program stops. With it, you catch the problem, show a friendly message, and keep running. This is how real apps stay alive.
What is try/except?
try/except is Python's error-handling mechanism. You put risky code in a try block. If an error (called an exception) occurs, Python jumps to the except block instead of crashing.
- โข
try:โ the risky code goes here - โข
except ExceptionType:โ code that runs if that error occurs - โข
except Exception as e:โ catch any error and get its message ine - โข
else:โ runs only if NO error occurred - โข
finally:โ always runs, whether or not there was an error (great for cleanup) - โขCommon exceptions:
ValueError,TypeError,FileNotFoundError,ZeroDivisionError
Think of it like Roblox respawn
In Roblox, when your character falls off the map, the game doesn't close โ you respawn. try/except is your code's respawn system. When something goes wrong, instead of the whole program dying, you catch the fall and safely recover.
How It Works
- โข1. Python tries every line in the
tryblock one by one - โข2. If a line raises an exception, Python immediately jumps to the matching
except - โข3. Lines after the error in
tryare skipped - โข4. If no exception occurs, the
exceptblock is skipped entirely - โข5.
finallyalways runs last โ use it to close files or database connections - โข6. You can have multiple
exceptblocks for different error types
Real-World Examples
- โขA banking app wraps every transaction in
try/exceptso a database error doesn't freeze the whole app - โขSpotify uses error handling when fetching album art โ if the image fails to load, it shows a placeholder
- โขYouTube catches
FileNotFoundErrorwhen a video has been deleted but someone tries to share the old link - โขWhatsApp catches
ConnectionErrorwhen your phone loses signal mid-message and queues the message to retry
Key Facts
- โขExceptions are objects in Python โ they have a
.argsand.__class__.__name__ - โขYou can raise your own exceptions:
raise ValueError("Age must be positive") - โขCatching bare
except:(no type) is bad practice โ it hides bugs you didn't intend to catch - โข
try/excepthas almost zero performance cost when no error occurs
Watch Out!
Never write except: pass โ this silently swallows all errors, including ones you didn't expect. Always catch specific exceptions (like ValueError) or at least except Exception as e: print(e) so you know what went wrong.
Remember
Use try/except wherever user input or external data (files, APIs, networks) is involved โ those are the places your code is most likely to get unexpected values.
What You Learned
- โข
try/exceptcatches errors so your program keeps running - โขCatch specific exception types โ never use bare
except: pass - โขUnlocks: robust user input handling, safe file reading, resilient API calls
Key Facts
- โExceptions are objects in Python โ they have a
.argsand.__class__.__name__ - โYou can raise your own exceptions:
raise ValueError("Age must be positive") - โCatching bare
except:(no type) is bad practice โ it hides bugs you didn't intend to catch - โ
try/excepthas almost zero performance cost when no error occurs
Real-World Examples
Remember
Use try/except wherever user input or external data (files, APIs, networks) is involved โ those are the places your code is most likely to get unexpected values.
Quick Quiz
try/except is for?