Viser kun
0:03
S… Speaker 2 (7- The finally Block)
Let's imagine this file exists,
0:05
S… Speaker 2 (7- The finally Block)
so we successfully open it for reading,
0:07
S… Speaker 2 (7- The finally Block)
but something goes wrong when we try to read data from this file.
0:11
S… Speaker 1 (7- The finally Block)
This could happen because of a hardware problem.
0:13
S… Speaker 1 (7- The finally Block)
Now we have a problem in this code.
0:15
S… Speaker 2 (7- The finally Block)
The problem is that we have opened this file for reading,
0:19
S… Speaker 2 (7- The finally Block)
but nowhere we have closed it.
0:21
S… Speaker 2 (7- The finally Block)
File handles are operating system resources,
0:23
S… Speaker 2 (7- The finally Block)
so whenever we get them,
0:25
S… Speaker 2 (7- The finally Block)
we should always release them.
0:26
S… Speaker 2 (7- The finally Block)
Otherwise, other processes may not be able to access these resources.
0:30
S… Speaker 2 (7- The finally Block)
So somewhere in this code,
0:33
S… Speaker 2 (7- The finally Block)
we should write reader .close.
0:36
S… Speaker 1 (7- The finally Block)
Now there's a problem with this implementation.
0:38
S… Speaker 1 (7- The finally Block)
Can you tell?
0:39
S… Speaker 1 (7- The finally Block)
The problem is that if this line throws an exception,
0:43
S… Speaker 2 (7- The finally Block)
the runtime will pass the control to this catch block,
0:47
S… Speaker 2 (7- The finally Block)
so this line will never get executed.
0:49
S… Speaker 2 (7- The finally Block)
Now what if we put this line after our try catch block?
0:52
S… Speaker 1 (7- The finally Block)
Now the reader variable is not recognized
0:57
S… Speaker 2 (7- The finally Block)
because we have declared it inside this try block,
1:00
S… Speaker 2 (7- The finally Block)
so it's only accessible in this block.
1:02
S… Speaker 2 (7- The finally Block)
But let's not worry about it for a second.
1:04
S… Speaker 2 (7- The finally Block)
The problem with this implementation is that sometime in the future,
1:07
S… Speaker 2 (7- The finally Block)
another developer might come here and write some extra code
1:12
S… Speaker 2 (7- The finally Block)
and this code may throw an exception.
1:14
S… Speaker 1 (7- The finally Block)
Again,
1:15
S… Speaker 2 (7- The finally Block)
our reader is not going to get closed.
1:18
S… Speaker 1 (7- The finally Block)
The proper way to handle this kind of scenarios is to use the finally
1:22
S… Speaker 1 (7- The finally Block)
block.
1:22
S… Speaker 1 (7- The finally Block)
So right after the catch block,
1:24
S… Speaker 2 (7- The finally Block)
we type finally
1:27
S… Speaker 2 (7- The finally Block)
Now in this block we can release external resources like file handles,
1:31
S… Speaker 2 (7- The finally Block)
database connections,
1:32
S… Speaker 2 (7- The finally Block)
network connections and so on.
1:34
S… Speaker 2 (7- The finally Block)
So here we should type reader .close
1:38
S… Speaker 2 (7- The finally Block)
but once again reader is not recognized because it's
1:42
S… Speaker 2 (7- The finally Block)
only accessible in the try block.
1:44
S… Speaker 2 (7- The finally Block)
To solve this problem we need to declare this before our try block.
1:48
S… Speaker 1 (7- The finally Block)
So we type file reader reader
1:52
S… Speaker 1 (7- The finally Block)
and then set it over here.
1:56
S… Speaker 2 (7- The finally Block)
And by the way,
1:57
S… Speaker 2 (7- The finally Block)
we cannot use var over here,
1:59
S… Speaker 2 (7- The finally Block)
because we can use var only when we set this to a new instance
2:03
S… Speaker 1 (7- The finally Block)
of a class.
2:04
S… Speaker 2 (7- The finally Block)
So the compiler knows that this object is an instance of the file
2:08
S… Speaker 1 (7- The finally Block)
reader class.
2:08
S… Speaker 1 (7- The finally Block)
In this case,
2:09
S… Speaker 2 (7- The finally Block)
we're not initializing this variable.
2:12
S… Speaker 2 (7- The finally Block)
So the compiler doesn't know what is the type of this variable.
2:15
S… Speaker 1 (7- The finally Block)
So,
2:16
S… Speaker 1 (7- The finally Block)
let's type file reader.
2:19
S… Speaker 1 (7- The finally Block)
Now,
2:20
S… Speaker 2 (7- The finally Block)
we have a compilation error.
2:22
S… Speaker 2 (7- The finally Block)
variable reader might not have been initialized.
2:25
S… Speaker 2 (7- The finally Block)
Because in Java,
2:26
S… Speaker 2 (7- The finally Block)
before we can access an object,
2:28
S… Speaker 2 (7- The finally Block)
we should always initialize it.
2:29
S… Speaker 2 (7- The finally Block)
So if this line doesn't get executed,
2:33
S… Speaker 2 (7- The finally Block)
if it throws an exception,
2:34
S… Speaker 2 (7- The finally Block)
we'll try to use an uninitialized object.
2:37
S… Speaker 1 (7- The finally Block)
To solve this problem,
2:38
S… Speaker 2 (7- The finally Block)
we need to explicitly set this to null.
2:42
S… Speaker 1 (7- The finally Block)
The compilation error is gone,
2:44
S… Speaker 2 (7- The finally Block)
now we have a different kind of error,
2:46
S… Speaker 2 (7- The finally Block)
we'll talk about that in a second,
2:47
S… Speaker 2 (7- The finally Block)
but first let's add an if statement.
2:50
S… Speaker 2 (7- The finally Block)
We want to call the close method only if the reader is not null.
2:53
S… Speaker 2 (7- The finally Block)
So if reader is not null,
2:56
S… Speaker 2 (7- The finally Block)
then close it.
2:58
S… Speaker 2 (7- The finally Block)
But what is this compilation error?
3:00
S… Speaker 1 (7- The finally Block)
On handle exception,
3:02
S… Speaker 2 (7- The finally Block)
IO exception.
3:04
S… Speaker 2 (7- The finally Block)
So the close method may also throw an IO exception.
3:07
S… Speaker 1 (7- The finally Block)
To solve this problem,
3:09
S… Speaker 2 (7- The finally Block)
we need to wrap this inside the try catch block.
3:12
S… Speaker 2 (7- The finally Block)
So we put the caret over here,
3:14
S… Speaker 2 (7- The finally Block)
press alt and enter,
3:16
S… Speaker 2 (7- The finally Block)
and then surround with try catch.
3:18
S… Speaker 1 (7- The finally Block)
So here's the final result.
3:22
S… Speaker 1 (7- The finally Block)
Now, this code looks really ugly.
3:25
S… Speaker 2 (7- The finally Block)
I have to admit that we have a much better exception handling structure in C sharp.
3:29
S… Speaker 1 (7- The finally Block)
Now, that aside,
3:30
S… Speaker 2 (7- The finally Block)
what I want you to take away here is that this find in the block will
3:34
S… Speaker 1 (7- The finally Block)
always get executed,
3:36
S… Speaker 2 (7- The finally Block)
whether we have an exception or not.
3:38
S… Speaker 2 (7- The finally Block)
If we don't have an exception,
3:39
S… Speaker 2 (7- The finally Block)
all the code inside the try block will get executed and
3:44
S… Speaker 2 (7- The finally Block)
then the runtime will pass control to the find in the block.
3:46
S… Speaker 1 (7- The finally Block)
If one of these lines throws an exception
3:50
S… Speaker 2 (7- The finally Block)
one of our catch blocks will get executed and then the finally block
3:54
S… Speaker 1 (7- The finally Block)
will get executed.
3:55
S… Speaker 2 (7- The finally Block)
So the finally block will always get executed no matter what.
4:00
S… Speaker 1 (7- The finally Block)
Now,
4:00
S… Speaker 2 (7- The finally Block)
this implementation is really ugly.
4:02
S… Speaker 2 (7- The finally Block)
In the next video,
4:03
S… Speaker 2 (7- The finally Block)
I'm going to show you a better way to release external resources.

Denne udskrift blev genereret af AI (automatisk talegenkendelse). Kan indeholde fejl ~ kontrollere mod den oprindelige lyd til kritisk brug. AI-politik

❤️ Elsker du STT.ai? Fortæl det til dine venner!
Oversigt
Klik på Summarize for at generere en AI resumé af denne udskrift.
Opsummering...
Spørg AI om denne transskription
Spørg om noget om denne udskrift! AI vil finde relevante sektioner og svar.