Custom Exception

noun

say it: “KUS-tum ek-SEP-shun” /ˈkʌstəm ɪkˈsɛpʃən/

An error type you make yourself by inheriting from Exception, named for exactly the problem it reports, such as OutOfStockError.

What is a custom exception?

A custom exception is your own error type, made by inheriting from Exception. Its name tells exactly what went wrong.

class OutOfStockError(Exception):
    pass

def buy(stock):
    if stock == 0:
        raise OutOfStockError("Sorry, sold out")

In a sentence

  • “A custom exception lets the caller catch sold-out items apart from every other error.”
  • “The shop raises a custom exception, OutOfStockError, when an item is sold out.”

Level: Journeyman · More Python terms at this level

Updated

In other subjects