Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed C# Objects!
      
    
You have completed C# Objects!
Preview
    
      
  Throw an exception to indicate that a method can't complete successfully.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      Let's get back to coding
up the MapLocation class.
                      0:00
                    
                    
                      We can make sure that users of this class
don't accidentally make a map location
                      0:03
                    
                    
                      that doesn't exist on the map.
                      0:07
                    
                    
                      The natural place to do this
validation is here in the constructor
                      0:09
                    
                    
                      because the constructor is called
when the object is created.
                      0:12
                    
                    
                      So we can actually stop
the object from being created
                      0:16
                    
                    
                      by causing this method to fail.
                      0:18
                    
                    
                      In order to do this validation,
                      0:21
                    
                    
                      the constructor will need to have
an instance of the map object.
                      0:22
                    
                    
                      So we'll add it to the list
of parameters here.
                      0:26
                    
                    
                      Now we can check this map location is
on the map by typing if map.OnMap and
                      0:29
                    
                    
                      then pass it this, open and
closing curly brace.
                      0:38
                    
                    
                      Let me go over what we just did here.
                      0:44
                    
                    
                      We're using the maps
OnMap method to determine
                      0:46
                    
                    
                      if the MapLocation being
constructed is on the map.
                      0:49
                    
                    
                      OnMap returns true if
the Point is on the map.
                      0:53
                    
                    
                      We only want to do something if
the Point is not on the map.
                      0:57
                    
                    
                      So we need to use the negation operator,
so that it reads, if not OnMap.
                      1:00
                    
                    
                      See how that works?
                      1:06
                    
                    
                      The word this is a special keyword
that refers to the current object.
                      1:08
                    
                    
                      You can use this from any method to get
the object that the method was called on.
                      1:13
                    
                    
                      In the case of a constructor method,
                      1:18
                    
                    
                      this refers to the object
that's being constructed.
                      1:20
                    
                    
                      You need to be careful when using
this in a constructor though.
                      1:23
                    
                    
                      You see an object is not fully constructed
until the constructor has returned.
                      1:26
                    
                    
                      There might still be some fields that
haven't been fully initialized and
                      1:31
                    
                    
                      using this too soon could
have unexpected results.
                      1:35
                    
                    
                      Just something to be aware of, the base
constructor is always called first.
                      1:40
                    
                    
                      So by now in the creation
of the map object,
                      1:45
                    
                    
                      all of the fields of the object
have been initialized.
                      1:48
                    
                    
                      So by the time the execution gets to here,
                      1:50
                    
                    
                      it's safe to pass this
to the OnMap method.
                      1:53
                    
                    
                      Now we need to decide what to do
if the point is not on the map?
                      1:57
                    
                    
                      Exceptions are used to tell
the calling code that a method was not
                      2:01
                    
                    
                      able to complete successfully.
                      2:05
                    
                    
                      In this case, the constructor is not
able to complete successfully and
                      2:07
                    
                    
                      the MapLocation object cannot be created.
                      2:11
                    
                    
                      We've already learned
how to catch exceptions,
                      2:14
                    
                    
                      now we need to throw an exception.
                      2:16
                    
                    
                      The most basic exception type
provided by the .NET Framework
                      2:19
                    
                    
                      is just named exception.
                      2:22
                    
                    
                      It's in the system namespace.
                      2:25
                    
                    
                      Exception types are classes
just like any other class.
                      2:27
                    
                    
                      To throw an exception we need to create a
new exception instance and then throw it.
                      2:30
                    
                    
                      I will show how to do that here.
                      2:35
                    
                    
                      We instantiate exceptions the same way
we instantiate every other class because
                      2:42
                    
                    
                      exceptions are really just classes.
                      2:47
                    
                    
                      The only thing new here is that
we're using this throw keyword
                      2:49
                    
                    
                      to throw the newly
created exception object.
                      2:54
                    
                    
                      Let's go see how this works in main.
                      2:57
                    
                    
                      Let's delete the code
we don't need here and
                      3:01
                    
                    
                      attempt to create a MapLocation
we know isn't on the map.
                      3:03
                    
                    
                      Let's say 20, 20 which is way off our map.
                      3:16
                    
                    
                      We also need to pass it the map object.
                      3:20
                    
                    
                      Now we know this could
potentially throw an exception.
                      3:22
                    
                    
                      We need to make a decision.
                      3:25
                    
                    
                      We can either try to handle the exception
here, or we can let the exception be
                      3:27
                    
                    
                      propagated back to the method
that called the method we're in.
                      3:31
                    
                    
                      In our case,
                      3:35
                    
                    
                      we're in the main method which is
the first method called in our program.
                      3:36
                    
                    
                      So there's no other method that could
handle the exception appropriately.
                      3:40
                    
                    
                      It's best to handle it here.
                      3:43
                    
                    
                      Otherwise, the program will crash and
                      3:45
                    
                    
                      the user will see a scary
looking error message.
                      3:47
                    
                    
                      To handle the exception,
                      3:50
                    
                    
                      we need to wrap the code that can throw
the exception with the try catch.
                      3:52
                    
                    
                      And the type of exception we need
to catch is system.exception.
                      4:00
                    
                    
                      We already have using system
here at the top of the file.
                      4:05
                    
                    
                      So we can just use the class name here.
                      4:08
                    
                    
                      Here in the catch block,
we'll print to the screen what happened.
                      4:14
                    
                    
                      So we'll say console.rightline.
                      4:18
                    
                    
                      That map location is not on the map.
                      4:24
                    
                    
                      Now when we run this code we'll see
our message printed to the screen.
                      4:31
                    
                    
                      We can ignore the warning about
the location variable not being used.
                      4:38
                    
                    
                      Here's our error message.
                      4:42
                    
                    
                      See how well that worked.
                      4:44
                    
                    
                      There is more to learn about exceptions
and how to use them in the next video.
                      4:45
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up