Classes-Exceptions Lesson in Python course


I’m stumped on this project even though the reviewer says the code is right and allows me to move to the next lesson. At the top of the code I define the class OutOfStock(Exception):. When the code runs, it prints the statement in the class definition. The exception never executes when the condition is met within the body of the program.

Here’s the project - Classes - Exceptions

Here’s the code

Define your exception up here

class OutOfStock(Exception):
print(“Exception called”)

print("\n an exception has been made")

Update the class below to raise OutOfStock

class CandleShop:
name = “Here’s a Hot Tip: Buy Drip Candles”
def init(self, stock):
self.stock = stock

def buy(self, color):
if self.stock[color] <= 0:
raise OutOfStock
self.stock[color] = self.stock[color] - 1

candle_shop = CandleShop({‘blue’: 6, ‘red’: 2, ‘green’: 0})

This should raise OutOfStock:‘green’)

Here’s the output - with error
Exception called
{‘red’: 2, ‘blue’: 6, ‘green’: 0}
{‘red’: 2, ‘blue’: 5, ‘green’: 0}
Traceback (most recent call last):
File “”, line 26, in‘green’)
File “”, line 17, in buy
raise OutOfStock


@oduffy, what page was that new users are supposed to read?


I don’t understand your question.


I’m working through this one at the moment and while I’m able to continue moving forward in the course, there seems to be something wrong with this particular exercise. Just typing ‘Raise’ doesn’t work if you test it for an error (uncomment buy Green) with the solution code. I believe that it needs a Try / Except block to function properly. I hope the team will examine the code.