1 00:00:00,410 --> 00:00:03,420 Let's get back to coding up the MapLocation class. 2 00:00:03,420 --> 00:00:07,230 We can make sure that users of this class don't accidentally make a map location 3 00:00:07,230 --> 00:00:09,230 that doesn't exist on the map. 4 00:00:09,230 --> 00:00:12,880 The natural place to do this validation is here in the constructor 5 00:00:12,880 --> 00:00:16,080 because the constructor is called when the object is created. 6 00:00:16,080 --> 00:00:18,920 So we can actually stop the object from being created 7 00:00:18,920 --> 00:00:21,340 by causing this method to fail. 8 00:00:21,340 --> 00:00:22,940 In order to do this validation, 9 00:00:22,940 --> 00:00:26,290 the constructor will need to have an instance of the map object. 10 00:00:26,290 --> 00:00:28,320 So we'll add it to the list of parameters here. 11 00:00:29,760 --> 00:00:37,600 Now we can check this map location is on the map by typing if map.OnMap and 12 00:00:38,980 --> 00:00:44,250 then pass it this, open and closing curly brace. 13 00:00:44,250 --> 00:00:45,660 Let me go over what we just did here. 14 00:00:46,680 --> 00:00:49,490 We're using the maps OnMap method to determine 15 00:00:49,490 --> 00:00:52,150 if the MapLocation being constructed is on the map. 16 00:00:53,230 --> 00:00:57,120 OnMap returns true if the Point is on the map. 17 00:00:57,120 --> 00:01:00,630 We only want to do something if the Point is not on the map. 18 00:01:00,630 --> 00:01:05,575 So we need to use the negation operator, so that it reads, if not OnMap. 19 00:01:06,880 --> 00:01:08,400 See how that works? 20 00:01:08,400 --> 00:01:13,440 The word this is a special keyword that refers to the current object. 21 00:01:13,440 --> 00:01:17,080 You can use this from any method to get the object that the method was called on. 22 00:01:18,160 --> 00:01:20,070 In the case of a constructor method, 23 00:01:20,070 --> 00:01:23,530 this refers to the object that's being constructed. 24 00:01:23,530 --> 00:01:26,440 You need to be careful when using this in a constructor though. 25 00:01:26,440 --> 00:01:31,380 You see an object is not fully constructed until the constructor has returned. 26 00:01:31,380 --> 00:01:35,500 There might still be some fields that haven't been fully initialized and 27 00:01:35,500 --> 00:01:39,170 using this too soon could have unexpected results. 28 00:01:40,220 --> 00:01:45,160 Just something to be aware of, the base constructor is always called first. 29 00:01:45,160 --> 00:01:48,000 So by now in the creation of the map object, 30 00:01:48,000 --> 00:01:50,910 all of the fields of the object have been initialized. 31 00:01:50,910 --> 00:01:53,380 So by the time the execution gets to here, 32 00:01:53,380 --> 00:01:55,987 it's safe to pass this to the OnMap method. 33 00:01:57,330 --> 00:02:00,800 Now we need to decide what to do if the point is not on the map? 34 00:02:01,950 --> 00:02:05,490 Exceptions are used to tell the calling code that a method was not 35 00:02:05,490 --> 00:02:07,470 able to complete successfully. 36 00:02:07,470 --> 00:02:11,530 In this case, the constructor is not able to complete successfully and 37 00:02:11,530 --> 00:02:14,620 the MapLocation object cannot be created. 38 00:02:14,620 --> 00:02:16,810 We've already learned how to catch exceptions, 39 00:02:16,810 --> 00:02:19,200 now we need to throw an exception. 40 00:02:19,200 --> 00:02:22,720 The most basic exception type provided by the .NET Framework 41 00:02:22,720 --> 00:02:23,970 is just named exception. 42 00:02:25,000 --> 00:02:27,170 It's in the system namespace. 43 00:02:27,170 --> 00:02:30,630 Exception types are classes just like any other class. 44 00:02:30,630 --> 00:02:35,610 To throw an exception we need to create a new exception instance and then throw it. 45 00:02:35,610 --> 00:02:37,275 I will show how to do that here. 46 00:02:42,207 --> 00:02:47,019 We instantiate exceptions the same way we instantiate every other class because 47 00:02:47,019 --> 00:02:49,980 exceptions are really just classes. 48 00:02:49,980 --> 00:02:54,230 The only thing new here is that we're using this throw keyword 49 00:02:54,230 --> 00:02:57,470 to throw the newly created exception object. 50 00:02:57,470 --> 00:02:58,860 Let's go see how this works in main. 51 00:03:01,290 --> 00:03:03,608 Let's delete the code we don't need here and 52 00:03:03,608 --> 00:03:06,601 attempt to create a MapLocation we know isn't on the map. 53 00:03:16,898 --> 00:03:20,595 Let's say 20, 20 which is way off our map. 54 00:03:20,595 --> 00:03:22,885 We also need to pass it the map object. 55 00:03:22,885 --> 00:03:25,545 Now we know this could potentially throw an exception. 56 00:03:25,545 --> 00:03:27,260 We need to make a decision. 57 00:03:27,260 --> 00:03:31,360 We can either try to handle the exception here, or we can let the exception be 58 00:03:31,360 --> 00:03:35,430 propagated back to the method that called the method we're in. 59 00:03:35,430 --> 00:03:36,310 In our case, 60 00:03:36,310 --> 00:03:40,050 we're in the main method which is the first method called in our program. 61 00:03:40,050 --> 00:03:43,630 So there's no other method that could handle the exception appropriately. 62 00:03:43,630 --> 00:03:45,460 It's best to handle it here. 63 00:03:45,460 --> 00:03:47,840 Otherwise, the program will crash and 64 00:03:47,840 --> 00:03:49,740 the user will see a scary looking error message. 65 00:03:50,820 --> 00:03:52,112 To handle the exception, 66 00:03:52,112 --> 00:03:55,648 we need to wrap the code that can throw the exception with the try catch. 67 00:04:00,755 --> 00:04:05,100 And the type of exception we need to catch is system.exception. 68 00:04:05,100 --> 00:04:08,430 We already have using system here at the top of the file. 69 00:04:08,430 --> 00:04:10,579 So we can just use the class name here. 70 00:04:14,764 --> 00:04:18,670 Here in the catch block, we'll print to the screen what happened. 71 00:04:18,670 --> 00:04:21,182 So we'll say console.rightline. 72 00:04:24,201 --> 00:04:29,033 That map location is not on the map. 73 00:04:31,685 --> 00:04:35,533 Now when we run this code we'll see our message printed to the screen. 74 00:04:38,184 --> 00:04:41,650 We can ignore the warning about the location variable not being used. 75 00:04:42,690 --> 00:04:44,320 Here's our error message. 76 00:04:44,320 --> 00:04:45,670 See how well that worked. 77 00:04:45,670 --> 00:04:48,950 There is more to learn about exceptions and how to use them in the next video.