Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C#

Hello! Please tell me why do we need "throw Exception" in the MapLocation class if we can only use "try catch"

Hello! Please tell me why do we need "throw Exception" in the MapLocation class if we can only use "try catch" block in the Game class and in the catch block we can write "catch{Exception}". So why do we write throw Exception in the Maplocation.class?

2 Answers

Steven Parker
Steven Parker
216,083 Points

The "throw" must be done at the point where the error is detected, and it is in the MapLocation class where the test is performed to check if the point is on the map.

The "try" and "catch" can be placed anywhere else in the code where the method that throws the exception is called. The ability for an exception to be handled in a different class from where it is thrown is one of the features of exceptions that make them useful.

Thank you very much!