Tip:
Highlight text to annotate it
X
And finally...
There is one more bit to the 'try' catch block
that we need to understand
and that is the 'finally' block.
So we have: 'try, catch, finally'.
There's three parts to it.
'Finally' is optional, you don't have to have a finally.
which is why I haven't put it on the board there
you can if you like.
'Finally' is always executed.
It's executed if no exception was raised.
It's executed if an exception was raised.
Okay?
So, if we dropped out with an exception was raised to the catch block
and there was a 'finally,' we'd jump to whatever was in the 'finally.'
Okay, so that is the third optional bit to a 'try' catch block.
So that what we get
is we can, taking our example with 'canine,'
we can see that will raise an exception
which will be handled in this catch block.
Once the catch block is finished
we'd jump, because there is a 'finally', to the code in the 'finally.'
This always happens.
And so you can see the output is to print the message from the exception,
to execute the 'finally' block
and then to execute the next line of code after the 'try' catch.
Okay?
So 'finally' is useful to always tidy up at the end.
So for example, the example we had this morning:
make sure the cash machine always gives back the card.
Well, you'd give back the card in the 'finally' block.
Having unwound the debit of the cash from the bank account
because the deduction went wrong.
But you'd always give the card back.
So that guarantees that you always give the card back,
or always do that piece of code.
And just to reinforce it
now we have a valid value for canines
so there is no exception raised
we can do 'makeNoise' for 'canine'.
The catch block is not executed
but the 'finally' always is.
You can see we've made a noise,
we've run the 'finally,'
and then we run the message.
'Finally' is always executed.
See if you've been listening.
What is an unchecked exception?
So close.
Give me an example of unchecked exception?
Null points are exception. Fantastic.
So what does a null point exception indicate?
Something is equal to null, and that was not expected.
It tends to mean
that you've got some rubbish in your code.
Why?
This is Java's philosophy.
Why is it saying, really, we shouldn't have one of these things?
There's no need to have it, yeah?
What it's saying really, is that you haven't handled things properly
you haven't guarded the lack of an object properly.
So your code is missing something.
So the philosophy in Java
is to raise that us an unchecked exception.
it's your problem to fix the code.
We won't force you to handle it.
It's a bug.
But the point is, an unchecked exception
is exactly that,
It's not checked, it is not handled.
You can handle it if you want.
But you don't have to
the compiler will let you
compile it without having to put a 'try' catch block.
So this
is true.
But actually the philosophy says:
that you haven't quite finished your code
because you should be coping with nulls
and you should be doing something about them.
Now if you think about it logically
if I called
If I started to change Dean's age to minus one
in this method here.
You could argue logically
that whoever typed the value in, in the user interface
typed the wrong value.
So at the user interface I should be dealing with
validation of values.
So there is a little bit of an ethos, a philosophy that says...
unchecked exceptions? There's something wrong with your code
that you need to sort out. You haven't done defensive code
or haven't done enough defensive code.
That's their philosophy, I don't necessarily agree with it.
Okay? Which is why they're unchecked.
Now we know that one,
What's a checked exception?
Should be easy.
Now here, remember what we are dealing with here?
We are trying to deal with the unexpected.
Okay?
The fuel sensor in my airplane doesn't work.
It's telling me there is no fuel
but I'm clearly at thirty thousand feet, I don't want to crash.
I don't want to turn the engine off.
I want to alert the pilot that something has gone wrong.
So a checked exception is all about
forcing you to deal with the problem.
Forcing you to have a 'try' catch.
And if it was an autopilot
and there was a dodgy fuel sensor,
the catch block would be flashing that little red light.
Because it has caught a fact that there is a problem.
And is trying to tell the user.
So in theory, your code is correct because you are handling it.
But you are expected to handle it.
That's the key thing with a checked exception.
When is the 'finally' block of code
for a 'try' statement executed?
WOW! Yes!
How many we got now? Is it three?
Something like that.
A 'finally' block is always executed.
If there is no exception, it's executed.
If there is an exception, it's executed.
We would use it, for example
especially in file handling,
so exactly how you've just done for the assignment.
In your 'try block', you would open the file
and you would do some processing
on each line.
That's good. We could get an exception here,
we could get one here.
In the catch
you'll want to handle 'IOException'
etcetera
and any other exceptions that occur.
The finally
would always make sure we close the file.
So that we don't have to close it in here.
because remember if it was the last bit of the 'try' block
and we got an exception
the file was open, but we got an exception reading the file
we need to close it somewhere still.
Because the last line of code won't be executed.
So the finally means that it will always be executed
and we'll be able to close the file.
That's why we put file handling in 'try, catch, finally.'