problem 6: first solution without throwing exceptions