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