Chaining Custom Exceptions

Mon 10 August 2026
class InitialError(Exception):
    pass

class DerivedError(Exception):
    pass

try:
    try:
        raise InitialError("Initial failure")
    except InitialError as e:
        raise DerivedError("Follow-up failure") from e
except DerivedError as final_error:
    print(final_error)
Follow-up failure


Score: 10

Category: chapter7_custom_exceptions

Read More

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

Custom Exception With Logging

Mon 10 August 2026
class DataValidationError(Exception):
    pass

try:
    raise DataValidationError("Invalid CSV column format")
except DataValidationError as e:
    print("Validation Error Logged:", e)
Validation Error Logged: Invalid CSV column format


Score: 10

Category: chapter7_custom_exceptions

Read More
Page 1 of 2

Next »