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

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
231,271 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!