Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Exceptions are everywhere in Java. Let's learn to love them. Let's learn how to throw and handle exceptions in our code, so we can explain when our code is being misused.
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
We left our code in
a pretty fragile state.
0:00
We could definitely add way too
many Pez to our dispenser and
0:03
have a big mess on our hands.
0:06
Now, luckily, Java provides a way for
0:08
us to let the user of our objects know
when they've done something wrong.
0:10
I'm talking about exceptions.
0:15
It's possible for us to tell the consumer
that we are having problems with how
0:17
they're using our object.
0:21
Now, stated another way,
0:23
we can let them know that
an exceptional event has occurred.
0:24
And it's possible for the user to be
able to handle or catch that exception.
0:28
The way we do this on the usage
side of things is what is known as
0:33
a try/catch block.
0:36
Now, unlike what this little guy here
might tell you, there actually is a try.
0:38
Let's go use it.
0:42
So Java comes with some built-in
exceptions and you can create your own.
0:44
Now, we'll cover creating custom
exceptions in a future course.
0:48
But for now, I'd like you just to
get familiar with how they work.
0:51
So since we're dealing with
a case where an argument that
0:54
was passed into our method will
actually break the object.
0:57
One exception that we could use is one
that's known as the illegal argument
1:00
exception.
1:04
So let's use it.
All right, so to do it,
1:05
let's rework this code a little bit here.
1:07
So instead of just letting the fill
happen, let's go ahead and
1:10
we'll store a new variable
called newAmount.
1:13
And we'll store the current pezCount
plus the pezAmount that was passed in.
1:16
Okay, now, if this new amount
is more than we can store, so
1:21
if this new amount is more
than we can store, right?
1:26
We know that there's a MAX_PEZ limit.
1:30
So if it's bigger than that, then we need
to let them know by throwing an exception.
1:31
Now, most exceptions take a message.
1:38
So what this looks like is you say throw,
and we're gonna make a new,
1:39
sorry, throw, we're gonna make
a new IllegalArgument, and
1:45
spelling matters here, Exception,
IllegalArgumentException.
1:50
And most of them take a message,
which we will push here, and
1:55
we'll say, Too many PEZ!!!!
2:00
So what will happen is this will be
thrown and we'll exit the method.
2:05
So the code will have
exited before it gets here.
2:09
So we can safely go ahead and
do this, right?
2:12
We know that this is safe.
2:17
So again,
we see what it would possibly be.
2:19
If it's more than what's allowed there,
we throw this exception.
2:23
So let's go see what happens, what it
looks like when an exception happens.
2:26
So let's save this.
2:30
And again, we're talking about the illegal
argument of this pezAmount is illegal.
2:32
Okay, so over here, let's go ahead and
let's say after our last
2:37
little loop here of testing things,
let's say dispenser.fill and
2:42
let's put a crazy number there,
let's fill with 400.
2:47
And what I wanna show off
is that at the bottom here,
2:52
because that exception happens,
that exception is going
2:55
to do what is known as bubbling,
it's gonna bubble up here.
2:59
This will never happen.
3:03
This line is never going to happen
because the exception is going
3:05
to be thrown here as well.
3:07
Okay, so let's go ahead and
run this and see.
3:09
So we'll say clear && javac
3:12
Example.java && java Example.
3:17
All right, so
we should see a pretty nasty stack trace.
3:24
So exception in thread main, Too many PEZ.
3:29
So we tried to fill it in and
it blew up, and
3:33
see here it's showing where it happened.
3:35
This is our stack trace that
happened in PezDispenser on line 18.
3:37
That's where we threw the exception at,
cool.
3:40
So instead of getting this
ugly error message back and
3:43
exiting the program hard like we did,
right?
3:46
Because this line, this will never happen,
didn't happen, the program just exited.
3:48
Just like the other method,
it just exited.
3:52
We can actually wrap this
in a try catch block, and
3:55
this will let us handle the exception and
continue.
3:57
Here, let me show you.
4:00
So we'll say try, and
we're going to open up some new scope and
4:03
I'm going to close some
new scope right away.
4:06
And let's go ahead and I want to move both
of these lines inside of that try scope,
4:09
that try block there.
4:14
Okay, and so what happens is it comes in
here into this and it will try it and
4:17
if not, it will catch anything and
you can be very specific here.
4:23
So we're going to say specifically I only
want to catch IIllegalArgumentExceptions.
4:28
Now typically I name each one
of these what they are there.
4:36
So iae, IllegalArgumentException.
4:39
So totally a style thing.
4:43
And what we can do is this code will run.
4:45
If something fails in here, specifically
if an illegal argument happens,
4:48
it will run this code here.
4:52
So let's say,
we'll say System.out.printline Whoa there!
4:53
And we will, let's just show that
you can get the message, right?
5:04
That catch there is
gonna pass in that iae,
5:08
The error was %s.
5:14
So what we can do is we
can say iae.getMessage and
5:17
that will get the message that we passed
into the constructor of that, right?
5:20
So remember we passed in this message,
Too many PEZ, so
5:26
we pass that in there, Too many PEZ.
5:29
All right, so let's run that again.
5:32
Cool, as well there,
the error was, Too many PEZ,
5:37
I forgot to put a new line there.
5:39
But what's cool is it didn't print this,
this will never happen, right?
5:41
Because it can't,
it's not going to get past this, and
5:44
it's going to run this
line here this code.
5:47
Awesome, so we've successfully blocked our
Pez dispenser from getting over filled.
5:49
Great, now we have a way of communicating
when our object is being used incorrectly.
5:55
Throwing and catching exceptions
is very common in Java, so
6:00
it's good to get yourself
familiarized with them.
6:03
Some newcomers to Java will often complain
about how many exceptions are used and
6:06
defined.
6:11
But it's part of what leads
to Java's famous stability.
6:11
You can actually define that
a method might throw in exceptions.
6:15
And then anyone who uses that method will
need to either protect against it or
6:18
throw it themselves.
6:22
We'll touch on these in
upcoming courses and
6:23
you'll encounter them in
the standard Java libraries.
6:25
If you'd like to see more information
on this, check out the teachers notes.
6:28
The exception type that we just used
IllegalArgumentException is called
6:31
a runtime exception, and therefore,
6:35
we don't need to declare
it at the method level.
6:37
By anticipating how things might go wrong,
we can provide a stable,
6:40
testable object that can easily
integrate into existing products.
6:44
So, now that we can fill, and dispense our
Pez dispenser, as well as know that if
6:49
people misuse it, will warn them,
I think we are done with our first object.
6:53
If what I warned you about comes true,
and you do indeed start imagining how
6:58
everything you see in real life
could be implemented as an object,
7:02
please head to the forum, use it
much like you would a support group.
7:06
We're all going through the same thing,
7:09
and a little comradery can really help
in situations like this, it gets better.
7:11
Make sure to check the teacher's notes and
extra credit section if you're looking for
7:16
more practice.
7:19
Also, thanks again for
your feedback and ideas.
7:20
It really is helping us to ensure that
we are building the best courses with
7:22
the skills you need and want.
7:27
Please do hang out in the community
forums, ask questions, answer questions.
7:29
And remember this little tidbit of
wisdom from this guy's dying breath,
7:34
pass on what you have learned.
7:38
Thank you so much for hanging out and
I hope you had as much fun as I did.
7:41
Looking forward to seeing you very soon.
7:44
See you at the next course,
right after this exercise.
7: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