Creating Custom Exception Classes
Mon 10 August 2026
class NegativeNumberError(Exception):
pass
def check_number(n):
if n < 0:
raise NegativeNumberError("Negative numbers are not allowed")
check_number(-3)
---------------------------------------------------------------------------
NegativeNumberError Traceback (most recent call last)
Cell In[1], line 8
5 if n < 0:
6 raise NegativeNumberError("Negative numbers are not allowed")
----> 8 check_number(-3)
Cell In[1], line 6, in check_number(n)
4 def check_number(n):
5 if n < 0:
----> 6 raise NegativeNumberError("Negative numbers are not allowed")
NegativeNumberError: Negative numbers are not allowed
Score: 10
Category: chapter7_custom_exceptions