Python Learning Series · @onlysamarrr · Day 25 / 30
Day 25 covers else and finally, plus raise and custom exceptions. You raise your own error with raise, and you create a custom exception class by inheriting from Exception.
Phase: Intermediate · Reels: 121/450 – 125/450
Day 25 covers else and finally, plus raise and custom exceptions. You raise your own error with raise, and you create a custom exception class by inheriting from Exception.
class AgeError(Exception):
pass
def set_age(age):
if age < 0:
raise AgeError("Age cannot be negative")
return age
try:
set_age(-5)
except AgeError as e:
print(e)
print(set_age(21))
Age cannot be negative 21
Age checks, payment validation and booking rules use custom exceptions in real apps.
Select (tick) an option, then tap Check answer to see instantly whether your answer is right or wrong, with an explanation. You can change your answer and check again.
Q1. How do you create a custom exception?
Q2. What does raise do?
Q3. Why is except: pass bad?
| 121/450 | Concept | else aur finally |
| 122/450 | Live demo | raise se apna error |
| 123/450 | Common mistake | Error chhupana galat |
| 124/450 | Quiz | Quiz: age validator |
| 125/450 | Interview / real life | Interview: custom exception |