6- Catching Multiple Types of Exceptions

Jun 18, 2026 03:55 · 4:28 · English · Whisper Turbo · 2 speakers
This transcript expires in 21 рӯзҳо. Навсозии барои захираи доимӣ →
Танҳо нишон додан
0:03
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
Sometimes we need to catch multiple types of exceptions.
0:06
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
For example,
0:07
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
after reading a file,
0:08
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
let's call reader .read.
0:11
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
This reads one character and returns its numeric value.
0:15
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
So let's store it in a variable called value.
0:18
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
Now here we have another compilation error because we haven't
0:22
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
handled the IO exception.
0:24
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
So this read method may throw an IO exception if it cannot read
0:28
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
data from this file.
0:29
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
To solve this problem,
0:30
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
we need to type another catch clause here.
0:33
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
So catch,
0:34
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
like here we want to catch an IO exception,
0:36
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
we give it a name,
0:38
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
and in this block,
0:40
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
we can print a message like,
0:42
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
could not read data.
0:44
S… Speaker 2 (6- Catching Multiple Types of Exceptions)
The exception is gone.
0:47
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
Now we have multiple catch blocks,
0:49
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
and each catch block targets a specific type
0:53
S… Speaker 2 (6- Catching Multiple Types of Exceptions)
of exception.
0:54
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
So only one of these will be executed.
0:57
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
In this case,
0:58
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
we try to open a file that doesn't exist,
1:01
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
so this line will throw a file not found exception,
1:04
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
and this catch block will catch that exception.
1:08
S… Speaker 2 (6- Catching Multiple Types of Exceptions)
Now,
1:09
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
the code inside this catch block will get executed,
1:11
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
and after that,
1:12
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
the control will move over here.
1:14
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
So this second catch block will get ignored,
1:18
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
okay?
1:18
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
So we have multiple catch blocks,
1:20
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
and each catch block targets a specific type of exception.
1:23
S… Speaker 2 (6- Catching Multiple Types of Exceptions)
Now,
1:24
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
does the order of these catch blocks matter?
1:27
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
Sometimes it does.
1:28
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
So if I move this catch block over here,
1:31
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
you see a compilation error.
1:33
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
The Java compiler is saying that we have already caught the
1:38
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
file not found exception.
1:39
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
What does this mean?
1:40
S… Speaker 2 (6- Catching Multiple Types of Exceptions)
Well,
1:40
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
let's look at the documentation for the file not found exception class.
1:44
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
So we type Java file not found exception.
1:48
S… Speaker 2 (6- Catching Multiple Types of Exceptions)
Here it is.
1:52
S… Speaker 2 (6- Catching Multiple Types of Exceptions)
Okay.
1:54
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
So this class extends the IOException class.
1:57
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
And that means,
1:59
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
if we have a catch clause for an IOException,
2:02
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
we don't need this FindNotFoundException.
2:05
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
This is the polymorphism principle of object -oriented programming.
2:09
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
We talked about this in the second part of this course.
2:12
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
Let me show you.
2:13
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
If I have a variable of type IOException,
2:16
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
let's call it X,
2:17
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
I can set this variable to a new instance of IOException or
2:21
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
any classes that extend the IOException class.
2:24
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
So we can set this to a new file not found exception.
2:29
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
This is polymorphism.
2:31
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
An object may take different forms.
2:34
S… Speaker 2 (6- Catching Multiple Types of Exceptions)
So,
2:35
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
if you have a catch clause,
2:38
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
to catch an I .O .Exception,
2:40
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
this catch clause can catch an I .O .Exception or any of its derivatives,
2:44
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
any classes that extend the I .O .Exception class.
2:47
S… Speaker 2 (6- Catching Multiple Types of Exceptions)
Okay?
2:48
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
Now sometimes we want to treat these exceptions the same way,
2:51
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
for example here we can display a generic error message,
2:54
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
but other times we want to handle them differently.
2:57
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
If that is the case,
2:58
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
then we need to bring back this catch clause and put
3:03
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
it before the first catch clause,
3:04
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
so now if we get a find not found exception,
3:08
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
we can print a message like,
3:09
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
file does not exist.
3:12
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
So we are giving the user a more specific error message.
3:16
S… Speaker 2 (6- Catching Multiple Types of Exceptions)
Now,
3:17
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
as another example,
3:19
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
let's create a new simple date format object
3:24
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
and call the parse method.
3:26
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
Now here we have another compilation error,
3:29
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
because this parse method may throw an exception of type parseException.
3:34
S… Speaker 2 (6- Catching Multiple Types of Exceptions)
Now,
3:35
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
we can put the carrot over here,
3:36
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
press alt and enter,
3:38
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
and have IntelliJ add a catch clause for us.
3:41
S… Speaker 2 (6- Catching Multiple Types of Exceptions)
There you go.
3:42
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
So it's right here.
3:43
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
Now we have three catch clauses.
3:47
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
Let's say we want to treat all of these the same way.
3:49
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
So first we can get rid of the file not found exception.
3:53
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
Now we have two catch clauses.
3:55
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
We can combine this using a vertical bar.
3:58
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
So right after IO exception,
4:00
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
we type a vertical bar,
4:02
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
then we type parse exception,
4:04
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
and with this we don't need the second catch clause.
4:10
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
So this block can catch exceptions of type ioException or
4:14
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
parseException.
4:15
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
So these are different ways to catch multiple exceptions.
4:18
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
The approach you take really depends on your application and its requirements.
4:22
S… Speaker 1 (6- Catching Multiple Types of Exceptions)
Next we're going to talk about the finally block.

This transcript was generated by AI (automatic speech recognition). May contain errors — verify against the original audio for critical use. AI policy

❤️ Шумо STT.ai- ро дӯст медоред? Ба дӯстонатон бигӯед!
Ҷамъбаст
Барои эҷоди ҷамъбасти AI- и ин нусхаи навиштаҷот, тугмаи ҷамъбастро пахш кунед.
Иттилоот...
Дар бораи ин нусхаи навиштаҷот аз AI пурсед
Дар ин ҷо ба шумо дар бораи ин раванд маълумот дода мешавад: Интихоби дурусти раванди эҷодӣ ва раванди таҳия.