Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects Delivering the MVP Exceptions

Why an Exception anyway (In this video's context)?

Hi, If we just mention in console that the letter has already been guessed earlier, and just control that the number of retries stays put, that should serve our purpose as well with lesser effort. My question is, is it just a good practice of programming, or is there another reason for throwing an exception in this context? If its just a good practice, please explain why is it so?

1 Answer

It would not be so good to send a message with the error to the console because you would be coupling your code logic with your presentation (terminal window). Imagine one day, you decide use the same code to have the game be in the browser: you would have to work around the code because it's not loose enough to be reusable. With exceptions, whoever calls the piece of code and gets the error, needs to handle it and as such it becomes more flexible.

so basically throwing an exception ensures that this will be handled in anyway, who so ever is working on the code. Right! Thanks!