Cameron McKenzie: Hey, I'm Cameron Mackenzie, and
I want to show you how to fix the top 10. Most
common runtime exceptions in Java. Runtime error
number one, this is the good old arithmetic
exception that will happen if you ever start
dividing by zero. And you can see here we've got
int y equals zero, and then we're dividing by zero
here, that's going to cause a problem. So you
know, you can always just do a little check, you
can always say, surround with an if statement and
check that, hey, if y is not equal to zero, don't
do any division. And all of a sudden, if I run
this application right now, boom, I don't get any
errors. I don't get any output, but I don't get a
runtime exception. So that's the arithmetic
exception. Let's take a look at this here. I think
we're setting things up for a null pointer
exception, go to a pointer exception there. Now
that's caused because we've got an object
initialized to know and then we try and call a
method on it. You can't call a method on something
that's nul, you can't get a property of something.
That's null, a null thing is no. So what do you do
here? Well, you can always do the null check, I'm
not a big fan of the null check butt, you know,
sometimes it's required. So you can say if data is
not equal to null, you know, that'll make the
problem go away. So if I now try and run this run
as a Java application, you'll see there's no air,
there's also no output, you know, might be better
to A, B, C, D, E, F, you and your, it might be
better just actually initialize the variable, for
goodness sakes, then it's not null. And there you
go and get the length of that data. So that's how
you can get rid of that null pointer exception.
Now, where are we going now? Oh, I thought this is
gonna have to do with the date time exception. But
no, this is a cast exception. We're trying to
figure out who the actors in our play are going to
be. And they don't like the cast here. You can see
right here, class cast exception. So we've got
this class called timestamp, we've got this class
called de I do believe, or class called date. I do
believe that if I looked at timestamp, it would
say timestamp extends date. So timestamp is a type
of date. However, polymorphism in Java is
unidirectional. So although a timestamp is a
special type of date, a date is not a timestamp,
just like a Mercedes is a car but not every Car is
a Mercedes. So we create a date, we try and cast
that date into a timestamp. And it says no, you
can't do that. You can't cast down the tree. So
when we run this, yes, it doesn't like who we're
casting in our play here, we get a class cast
exception. But you know, how do you avoid that?
Well, don't do casting, we could maybe make this a
date object. Right? You know, that might be one
way to get rid of it. So just do things properly.
But I think one of the things that might actually
be kind of interesting is we could probably flip
this around. So we did timestamp time equals how
do we create a new timestamp?
So we need to create a new timestamp, what system
current time Millis all we need.
Okay, so we create a timestamp. So we've got the
timestamp first the timestamp is a type of date.
And then we want the get the date today, we could
actually do the cast here. And so this will
actually work. So we can say date. Time. And now
since the date is a type of time that cast will
actually work. So watch if I run this now,
actually get the date. So there you go. A little
bit on the a unidirectional polymorphism of a
Java. Okay, and I think I mentioned a date time
exception might be coming up. And here it is, if I
run this code, boy, look at that date, time format
runtime exception right there. And so important
temporal type, actually take a little bit of a
look at that unsupported temporal type, you're
actually defining that that's actually a special
subclass of the date time exception. And why are
we getting that? Well, we're getting that because
looks like my system doesn't support the RFC date
time. But there's a bunch of different date times.
I'm gonna just get an ISO date. Right? You use a
data type that your application supports. We run
that, and I don't know what this kind of nonsense
is down there. And that's not supposed to be
there. That's for a future exception. anyways, you
can see that when we use a supported time format,
well, all those problems go away. So that's how
you get rid of that date time exception. Okay,
what's happening here? Now look, we got an array
with five elements, we all know what's going to
happen here, boom, we get an array index out of
bounds exception. Now, the array is created with
five elements, but it uses zero based counting. So
even if I use index five there, I'll still get an
array index out of bounds exception, the last
index, an array that has a size of five, the last
element is actually element number four. So only
if I make data for there, will we be within the
range? You know, how do you avoid that? I don't
know, maybe use a collection class like a list.
Because a list can expand dynamically. Optionally,
maybe just surround around this with a little if
statement. And you could always check that, you
know, if the index is going to be less than five,
so index equals six. And then if indexes less than
five, then change the item at that index. So there
we go, this code looks a little bit safer right
now. And there we go. And we don't get the
exception. But that key is that yeah, you can't,
you can't set an element at an index greater than
the size of the array. So be careful there. What
also we got, run this as a little java
application, see what happens, I think I know
what's happening here. Negative array size, except
you can't have a negative array, you got to stick
with the whole integer numbers, you can't have an
array that is the square root of negative two or
something like that. So that was the problem that
was negative, if you just make that positive, then
why not be positive, we should all be more
positive, there we go, the error goes away runs
without a runtime exception. Now let's take a look
at this one, this one actually looks pretty
interesting, we've got an array of numbers create
an array of numbers of type big int. And if we
actually look at big int, you'll actually see that
big int extends number it is a type of number. And
so this is all valid here. And then we say okay,
well, it's a throw in a double, right? If I take a
look at that double class, you'll see that double
extends number. So a double is a number. But when
I actually run this code, boom, array store
exception. And even though this is an array of
numbers, well, we actually created an array of big
integers. And we then try and put a double into
it. And even though this says it holds numbers,
under the covers, it can only accept a big
integer. So how do we change that? Well, I guess
we could always just set Element Zero to be a big
int. Let's copy that. Paste it in there instead of
double.
Gotta use the do keyword. And then when we run
this, there we go, that error goes away. And then
of course, actually have to call the constructor
properly here. And how do you create a big
integer? Okay, a big integer 12345. There we go.
Okay, now we've got a big integer at that element,
and everything will run fine. So you can see this
runs without throwing an exception, you know,
another another approach might this might be just
to make that number, right. And if we make that a
new number, then, you know, we could do double dot
value of I'm going to have 12345 before. And now
when we run it, it will actually work because this
is actually an element an array of number objects,
not big M's. So there are a couple approaches to
fixing that particular error. Okay, what have we
got here? Oh, I got an array, you know, asked my
boss for a raise the other day and he said, No,
just use a list. Instead, we've got a list right
there of integers. So we've taken this array. We
use the arrays as list method, and we've returned
it as a list which is a little easier to work
with. And then we try and add a new integer to
this list, which will usually work, but in this
case, we get unsupported operation exception. And
that's because well, boom over here, that add
method won't work. Because when you get a list
from the as list method, well, that particular
method is read only. So you can't add to a read
only this. So how do you fix this? Well, you get
rid of that add, if you want to do some
manipulation, do some manipulation to that data
list beforehand. And then after you've manipulated
the data list beforehand, so we can put 1010 up
there. Do that first. And then use the as list
method. So you don't have to manipulate the list
after you get it from that Arrays class. Okay,
over here, looks like we have a hash set created.
And then we asked to get the next element in the
array. Hmm. Okay, looks like it's gonna fail. So
let's run that. And yes, see no such mela element
exception. And so here we create an array hash
set, but there's nothing in it. And so we say,
hey, get this the iterator for the set, and then
get me the next element. Then the virtual machine
says, well, there's no elements in it. So you
can't get the next one that causes that exception.
So that's a little bit of an annoying, sometimes
you might create a set and expect it to have
things in it, and it doesn't and all of a sudden,
you're getting an exception. It's not even at the
hash. That's not It's not null, but it's empty.
And so how can you get rid of this particular
error? Well, you can see the code down here
already, it says, check to see if it's empty. And
so check to see if your hash set is empty before
we get the next element. And if it's not empty,
then call next. And if it is empty, well don't
manipulate it. So running this code here, makes
that error go away. And look, here's the last one.
I thought there were 10. But I can see that
runtime exception 09. That's kind of weird. And
what do we have here? I think this runs along
counter with the arrays, unsupported exception
earlier, makes up this is a concurrent
modification exception, you can see that we've got
well we created a list, we added an element to it,
we decided to iterate through the list, there is
something in it. So iterating through the list
isn't a problem. So we can call iterator dot next
and do something with it. But you notice in here,
they get that list servers. And they try to add to
it while they're iterating over it. And you can't
do that. If you have if you are iterating over a
list using the iterator. You cannot manipulate the
underlying list holding the elements. And that's
what's happening here. They're trying to add Jetty
to that list of application servers to complement
Tomcat. You can't do that. You could do that
afterwards. I could throw that down there for run
the application. And the error goes away. But you
can't do it inside of that iterator. Otherwise,
you get that concurrent modification exception.
And there you go. That's how easy it is to fix the
10 most common runtime exceptions in Java