#include #include class Fridge { public: Fridge(float capacity) : capacity {capacity}, used {} { if (capacity < 0) { throw std::logic_error{"Fridge cant have negative space"}; } } void add_stuff(float amount) { if (used + amount > capacity) { throw std::logic_error{"Fridge full. Cant add more!"}; } used += amount; } private: float capacity; float used; }; int main() { try { Fridge f {-1}; } catch (std::logic_error& e) { std::cout << e.what() << std::endl; } Fridge f {8}; f.add_stuff(3); f.add_stuff(3); try { f.add_stuff(3); } catch (std::logic_error& e) { std::cout << e.what() << std::endl; } }