5- Catching Exceptions

Jun 18, 2026 03:47 · 3:51 · English · Whisper Turbo · 2 المتكلمون
تنتهي مدة هذا النص في 21 أيام. تحسين التخزين الدائم →
العرض فقط
0:03
S… Speaker 2 (5- Catching Exceptions)
Now let's see how we can cache exceptions.
0:05
S… Speaker 2 (5- Catching Exceptions)
So let's create a file reader object.
0:08
S… Speaker 1 (5- Catching Exceptions)
Reader,
0:09
S… Speaker 2 (5- Catching Exceptions)
we set it to a new file reader.
0:11
S… Speaker 2 (5- Catching Exceptions)
And here we type file .txt,
0:14
S… Speaker 2 (5- Catching Exceptions)
the name doesn't matter.
0:15
S… Speaker 2 (5- Catching Exceptions)
Now right after this line,
0:17
S… Speaker 2 (5- Catching Exceptions)
we print a message like file open.
0:20
S… Speaker 2 (5- Catching Exceptions)
Now we have a compilation error because we haven't handled the file
0:24
S… Speaker 2 (5- Catching Exceptions)
not found exception.
0:25
S… Speaker 1 (5- Catching Exceptions)
To do this,
0:26
S… Speaker 2 (5- Catching Exceptions)
we should wrap this code inside a try block.
0:29
S… Speaker 1 (5- Catching Exceptions)
So we type try
0:31
S… Speaker 2 (5- Catching Exceptions)
followed by curly braces to indicate a block of code.
0:34
S… Speaker 2 (5- Catching Exceptions)
Then we move these lines inside this try block.
0:38
S… Speaker 1 (5- Catching Exceptions)
Now,
0:39
S… Speaker 2 (5- Catching Exceptions)
right after the try block,
0:41
S… Speaker 1 (5- Catching Exceptions)
we type the catch clause.
0:43
S… Speaker 2 (5- Catching Exceptions)
And here in parentheses,
0:44
S… Speaker 2 (5- Catching Exceptions)
we specify the type of exception we want to catch.
0:47
S… Speaker 2 (5- Catching Exceptions)
In this case,
0:48
S… Speaker 2 (5- Catching Exceptions)
file not found exception.
0:50
S… Speaker 2 (5- Catching Exceptions)
Then we give it a name.
0:53
S… Speaker 2 (5- Catching Exceptions)
By convention,
0:53
S… Speaker 2 (5- Catching Exceptions)
we call this object x,
0:55
S… Speaker 2 (5- Catching Exceptions)
as in short for exception.
0:56
S… Speaker 2 (5- Catching Exceptions)
So this x object is an instance of the file not found
1:01
S… Speaker 1 (5- Catching Exceptions)
exception.
1:01
S… Speaker 2 (5- Catching Exceptions)
It's an object that contains information about this exception.
1:04
S… Speaker 1 (5- Catching Exceptions)
So,
1:06
S… Speaker 2 (5- Catching Exceptions)
here we add another block,
1:08
S… Speaker 2 (5- Catching Exceptions)
and in this block,
1:09
S… Speaker 2 (5- Catching Exceptions)
we print a friendly message to the user.
1:11
S… Speaker 1 (5- Catching Exceptions)
Something like,
1:12
S… Speaker 2 (5- Catching Exceptions)
file does not exist.
1:15
S… Speaker 1 (5- Catching Exceptions)
Now,
1:16
S… Speaker 2 (5- Catching Exceptions)
let's run the program.
1:17
S… Speaker 1 (5- Catching Exceptions)
Our program didn't crash.
1:21
S… Speaker 2 (5- Catching Exceptions)
This is the proper way to handle checked exceptions.
1:25
S… Speaker 2 (5- Catching Exceptions)
Now, we can also get the message from this exception object.
1:28
S… Speaker 1 (5- Catching Exceptions)
So,
1:29
S… Speaker 1 (5- Catching Exceptions)
we type x dot get message.
1:32
S… Speaker 2 (5- Catching Exceptions)
This returns a string,
1:34
S… Speaker 2 (5- Catching Exceptions)
so we can print it on the terminal,
1:36
S… Speaker 1 (5- Catching Exceptions)
file dot txt,
1:38
S… Speaker 2 (5- Catching Exceptions)
no such file or directory.
1:40
S… Speaker 2 (5- Catching Exceptions)
This error message is defined in the file not found exception class.
1:44
S… Speaker 1 (5- Catching Exceptions)
Okay?
1:45
S… Speaker 1 (5- Catching Exceptions)
Now,
1:46
S… Speaker 2 (5- Catching Exceptions)
what happened to this statement over here?
1:50
S… Speaker 2 (5- Catching Exceptions)
We tried to print file open,
1:52
S… Speaker 2 (5- Catching Exceptions)
but we are not seeing that message.
1:53
S… Speaker 2 (5- Catching Exceptions)
Here's the reason.
1:55
S… Speaker 2 (5- Catching Exceptions)
When a line throws an exception,
1:57
S… Speaker 2 (5- Catching Exceptions)
the control moves to the catch block for that exception.
2:01
S… Speaker 2 (5- Catching Exceptions)
So the code that we have after the offending line will
2:05
S… Speaker 2 (5- Catching Exceptions)
not be executed.
2:06
S… Speaker 1 (5- Catching Exceptions)
However,
2:07
S… Speaker 2 (5- Catching Exceptions)
if I take this line and put it after our try catch block,
2:10
S… Speaker 1 (5- Catching Exceptions)
it will get executed.
2:13
S… Speaker 1 (5- Catching Exceptions)
Take a look.
2:14
S… Speaker 1 (5- Catching Exceptions)
So now we have file open.
2:18
S… Speaker 2 (5- Catching Exceptions)
So this is how we can catch exceptions in Java.
2:22
S… Speaker 2 (5- Catching Exceptions)
Now let me show you a trick.
2:23
S… Speaker 2 (5- Catching Exceptions)
I'm going to take this line out of our try block,
2:26
S… Speaker 1 (5- Catching Exceptions)
put it over here.
2:28
S… Speaker 2 (5- Catching Exceptions)
and then delete the rest of this code.
2:31
S… Speaker 2 (5- Catching Exceptions)
So let's say you're creating a file reader object and the
2:35
S… Speaker 2 (5- Catching Exceptions)
compiler is complaining that we haven't handled this checked exception.
2:38
S… Speaker 1 (5- Catching Exceptions)
Now,
2:39
S… Speaker 2 (5- Catching Exceptions)
we don't have to manually type a try catch block.
2:42
S… Speaker 2 (5- Catching Exceptions)
We can put the carrot over here,
2:43
S… Speaker 2 (5- Catching Exceptions)
press alt and enter and select surround with
2:48
S… Speaker 1 (5- Catching Exceptions)
try catch.
2:48
S… Speaker 1 (5- Catching Exceptions)
Isn't that beautiful?
2:51
S… Speaker 2 (5- Catching Exceptions)
So IntelliJ automatically created this try catch block for us.
2:55
S… Speaker 2 (5- Catching Exceptions)
Now here we're calling the print stack trace method of this exception object.
2:59
S… Speaker 2 (5- Catching Exceptions)
This will show us something like what you saw before.
3:02
S… Speaker 1 (5- Catching Exceptions)
So we got an exception of type file not
3:06
S… Speaker 2 (5- Catching Exceptions)
found exception.
3:07
S… Speaker 2 (5- Catching Exceptions)
Here's the error associated with this exception.
3:10
S… Speaker 2 (5- Catching Exceptions)
Now look at the stack trace.
3:11
S… Speaker 2 (5- Catching Exceptions)
This exception was thrown from the file input stream
3:15
S… Speaker 1 (5- Catching Exceptions)
class.
3:16
S… Speaker 2 (5- Catching Exceptions)
This is not part of our project.
3:17
S… Speaker 2 (5- Catching Exceptions)
This is part of the standard Java library.
3:20
S… Speaker 2 (5- Catching Exceptions)
So in our show method,
3:22
S… Speaker 2 (5- Catching Exceptions)
when we try to create a new file reader,
3:25
S… Speaker 2 (5- Catching Exceptions)
the constructor of the file reader was called and then internally this class
3:29
S… Speaker 2 (5- Catching Exceptions)
works with another class called file input stream.
3:32
S… Speaker 2 (5- Catching Exceptions)
Here we have a few method calls and this is where the exception
3:36
S… Speaker 2 (5- Catching Exceptions)
was thrown.
3:37
S… Speaker 2 (5- Catching Exceptions)
So sometimes you see a long slack trace because of the method calls
3:41
S… Speaker 2 (5- Catching Exceptions)
in the Java standard library or other libraries that you use.
3:45
S… Speaker 2 (5- Catching Exceptions)
Next we're going to talk about catching multiple types of exceptions.

هذا النص تم توليده بواسطة الذكاء الاصطناعي (التعرف التلقائي على الكلام). قد يحتوي على أخطاء - تحقق من الصوت الأصلي للاستخدام الحرج. سياسة الذكاء الاصطناعي

❤️ هل تحب STT.ai؟ أخبر أصدقاءك!
موجز تنفيذي
انقر على ملخص لإنشاء ملخص للذكاء الاصطناعي لهذا النص.
وباختصار...
اسأل AI عن هذا النص
وإذا سألت عن أي شيء في هذا النص، سيجد المحقق الخاص الأقسام ذات الصلة ويجيبك.