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 …

Category: chapter7_custom_exceptions

Read More
Page 5 of 20

« Prev Next »