Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
The IndexOutOfRangeException is the array's way of telling us that we've tried to get or set an index that doesn't exist in the array. This is easily avoided.
-
0:00
At the end of the last video we left the path class in an unusable state.
-
0:04
We set the accessibility of the path field to private
-
0:07
which means no other classes can access it, but
-
0:11
other classes still need to get some information about the path.
-
0:14
For example as an invader comes down the path we need to know
-
0:18
where on the map it is moved to so that we can determine if it's in range of a tower.
-
0:23
Let's add a method to the path class the returns a map location.
-
0:28
We'll call it, get location at.
-
0:30
It will take as a parameter the number of steps down the path they are.
-
0:36
We'll call this parameter path step.
-
0:39
The path step variable can work as an index into the path array.
-
0:43
So if you're at step zero in the path,
-
0:45
you'll be at the map location at index zero in the path array.
-
0:49
Remember, we can get the item at any index of the array.
-
0:53
So we can say, return path at path step, but we have a problem now.
-
1:01
And it's something we need to be aware of, every time we use an array or
-
1:04
a collection of any sort.
-
1:06
What happens if we ask for an index that isn't in the array.
-
1:11
Let's check this out in the C Sharp repl.
-
1:16
So go to the repl by typing C Sharp.
-
1:20
And say for example we have an array that's ten items long.
-
1:24
We'll make this an integer array.
-
1:34
This means that the only valid indexes are zero through nine.
-
1:39
If we ask for the item at index ten or
-
1:41
higher, then we'll cause the array class to throw an index out of range exception.
-
1:47
We can catch this exception though.
-
1:50
Let's see what that looks like.
-
1:57
So wrap the return statement in a try catch block.
-
2:06
And the type of exception we want to catch is
-
2:11
System.IndexOutOfRangeException.
-
2:18
Now, we need to decide what to do when this exception happens.
-
2:22
The return type of this method says we need to return a map location, but
-
2:27
what map location should we return in the event of an exception.
-
2:32
There isn't an obvious answer to this question.
-
2:35
What we can do is return something that tells the call of this method that there
-
2:39
is no map location at the step in the path.
-
2:41
This is a good place to return null.
-
2:44
Remember null represents the absence of a value.
-
2:48
And that's precisely what's going on here.
-
2:50
So let's return null here.
-
2:53
Now if the path array doesn't contain a map location for
-
2:55
the paths that passed in we'll catch the index out of range exception and
-
3:00
return null instead There's something you should know about exceptions though.
-
3:06
It's generally considered bad form to cause an exception to be thrown,
-
3:10
when it could easily be avoided.
-
3:12
The main reason for this, is that exceptions can be expensive.
-
3:17
No I don't mean that your bank account will be charged whenever you use them.
-
3:21
What I mean, is in each second of time a computer can only do so much work.
-
3:27
We should try to avoid making the computer work harder than it needs to,
-
3:31
especially if it can be avoided.
-
3:34
Some operations in programming are more expensive than others.
-
3:37
In terms of the amount of work required to complete them.
-
3:40
For example, multiplication and
-
3:43
division are generally more expensive than addition and subtraction.
-
3:48
When an exception is thrown some amount of work is performed to
-
3:51
construct the exception.
-
3:53
The exception is then communicated all the way back up the stack of method calls,
-
3:57
to find an appropriate catch clause that can handle it.
-
4:01
This means, if we think an exception could be thrown,
-
4:04
then we should consider ways to avoid causing the exception.
-
4:07
In this particular, case we can easily avoid this exception.
-
4:12
We can just check the path array contains the index requested
-
4:15
before we attempt to retrieve it from the array.
-
4:19
Instead of using try catch, we can use an if else statement instead.
-
4:24
So I'll say if Pathstep Is less
-
4:30
than half.length, then return path at path step.
-
4:37
Otherwise, or
-
4:41
else, Return null.
-
4:45
See, now we'll only try to attempt to access the array,
-
4:48
if we know that the path step is a valid index.
-
4:52
If for example the length of the path array was ten and
-
4:55
we passed an integer that's ten or greater,
-
4:59
this method would return null because the array only has indexes zero through nine.
-
5:06
You might be wondering, what will happen if path step is a negative integer?
-
5:11
In that case the index out of range exception will still be thrown.
-
5:16
You see that really would be an exceptional case.
-
5:19
It's conceivable the invaders might attempt to move off the end of the path,
-
5:23
but if they attempted to go to a negative path step
-
5:27
that would mean something is terribly wrong.
-
5:29
That would be a good reason to throw an exception.
-
5:33
That exception would be propagated all the way back to the main method of the program
-
5:37
and we've learned that there is a bug in the code.
-
5:40
Again, knowing when and how to use exceptions can take a bit of thought.
-
5:44
We'll learn more strategies for
-
5:46
dealing with exceptions and other error handling techniques in other courses.
You need to sign up for Treehouse in order to download course files.
Sign up