🧩Exception
Also known as Exception Handling, Try Catch, try/except
A signal that something broke mid-run. Code can catch it so the app does not crash.
Think of it like
Think of it like a net under a high rope. If the walker slips, the net catches them.
Example
A sign-up form tries int(age). When Jack types twelve, the form catches the ValueError and asks for a number.
Why it matters
Real input is messy and networks fail. Catching exceptions keeps one bad case from taking the whole app down.
Definition
Example code
python
try:
age = int("twelve")
except ValueError:
print("Please type a number")
# prints: Please type a numberCommon mistake
Key takeaways
- 1Raised on trouble. Something failed while the code ran.
- 2Catch what you expect. Name the kind of error you can handle.
- 3Never hide it. Log it or tell the user.