Lesson 15: raise & validation
Until now we caught errors Python raised. Now we'll raise an error ourselves with raise — useful when input is invalid (like a negative age). You write raise ValueError("message") to stop immediately and explain what went wrong. A common pattern is the guard clause: a check at the start of a functio
raise is like a referee raising a flag and blowing the whistle: 'Stop! That's not allowed!'. A guard clause is the check at the entrance — if the input is invalid, you stop right away before continuing.
- raise
- A statement that raises an exception on purpose — e.g. when input is invalid.
- guard clause
- A check at the start of a function that raises if input is invalid, stopping early.
- validation
- Checking that data is valid before continuing to use it.