10- Re-throwing Exceptions
Jun 18, 2026 09:19
· 3:07
· English
· Whisper Turbo
· 2 רמקולים
תעתיק זה פג תוקף ב 22 ימים.
שדרוג לאחסון קבוע →
מציג בלבד
0:03
S…
Speaker 1 (10- Re-throwing Exceptions)
In this program,
0:04
S…
Speaker 1 (10- Re-throwing Exceptions)
we are depositing a negative value in our account.
0:06
S…
Speaker 1 (10- Re-throwing Exceptions)
We catch an IO exception and then print the stack trace.
0:10
S…
Speaker 1 (10- Re-throwing Exceptions)
So when we run our program,
0:12
S…
Speaker 1 (10- Re-throwing Exceptions)
we see the stack trace.
0:14
S…
Speaker 2 (10- Re-throwing Exceptions)
Now,
0:15
S…
Speaker 1 (10- Re-throwing Exceptions)
in real applications,
0:16
S…
Speaker 1 (10- Re-throwing Exceptions)
when we catch these exceptions,
0:18
S…
Speaker 1 (10- Re-throwing Exceptions)
quite often we want to log them somewhere.
0:20
S…
Speaker 1 (10- Re-throwing Exceptions)
We want to store them in a file or in a database.
0:23
S…
Speaker 1 (10- Re-throwing Exceptions)
So sometime in the future,
0:24
S…
Speaker 1 (10- Re-throwing Exceptions)
we can come back and see what errors we had in our application.
0:28
S…
Speaker 1 (10- Re-throwing Exceptions)
Now, login is a completely separate topic.
0:30
S…
Speaker 1 (10- Re-throwing Exceptions)
Let's not worry about it for now.
0:31
S…
Speaker 1 (10- Re-throwing Exceptions)
Instead,
0:32
S…
Speaker 1 (10- Re-throwing Exceptions)
let's simulate logging by printing a message on a terminal.
0:35
S…
Speaker 1 (10- Re-throwing Exceptions)
So instead of printing the stack trace,
0:37
S…
Speaker 1 (10- Re-throwing Exceptions)
I'm going to print logging.
0:39
S…
Speaker 1 (10- Re-throwing Exceptions)
Now let's run our program.
0:41
S…
Speaker 1 (10- Re-throwing Exceptions)
So we see the login message,
0:45
S…
Speaker 1 (10- Re-throwing Exceptions)
but something is missing.
0:46
S…
Speaker 1 (10- Re-throwing Exceptions)
We're not telling the user that an error occurred.
0:49
S…
Speaker 1 (10- Re-throwing Exceptions)
So let's imagine the user tries to deposit a negative value.
0:53
S…
Speaker 1 (10- Re-throwing Exceptions)
They click the deposit button,
0:55
S…
Speaker 1 (10- Re-throwing Exceptions)
but nothing happens.
0:56
S…
Speaker 1 (10- Re-throwing Exceptions)
The application is not crashing.
0:57
S…
Speaker 1 (10- Re-throwing Exceptions)
It's not displaying an error either.
1:00
S…
Speaker 1 (10- Re-throwing Exceptions)
So, the reason this is happening is because we're catching
1:04
S…
Speaker 1 (10- Re-throwing Exceptions)
this exception over here.
1:05
S…
Speaker 1 (10- Re-throwing Exceptions)
So we handle it and then the application will continue the normal execution.
1:09
S…
Speaker 1 (10- Re-throwing Exceptions)
So in situations like this,
1:11
S…
Speaker 1 (10- Re-throwing Exceptions)
we should re -throw that exception so somewhere else in the application we
1:16
S…
Speaker 1 (10- Re-throwing Exceptions)
can get it and display a generic error message.
1:18
S…
Speaker 1 (10- Re-throwing Exceptions)
So in this catch block,
1:20
S…
Speaker 1 (10- Re-throwing Exceptions)
we type throw e.
1:23
S…
Speaker 1 (10- Re-throwing Exceptions)
We are re -throwing this exception object.
1:27
S…
Speaker 1 (10- Re-throwing Exceptions)
Now we have a compilation error.
1:30
S…
Speaker 1 (10- Re-throwing Exceptions)
the Java compiler is saying that we haven't handled the IO exception here.
1:34
S…
Speaker 1 (10- Re-throwing Exceptions)
In this case,
1:35
S…
Speaker 1 (10- Re-throwing Exceptions)
we don't want to wrap this inside the try catch block because when we throw an exception,
1:39
S…
Speaker 1 (10- Re-throwing Exceptions)
we want the color of this code to handle it.
1:42
S…
Speaker 1 (10- Re-throwing Exceptions)
So to fix this problem,
1:44
S…
Speaker 1 (10- Re-throwing Exceptions)
we specify this exception in the declaration of this method.
1:48
S…
Speaker 1 (10- Re-throwing Exceptions)
So we type throws IO exception.
1:52
S…
Speaker 1 (10- Re-throwing Exceptions)
Now we can also have IntelliJ do this for us.
1:56
S…
Speaker 1 (10- Re-throwing Exceptions)
So let me delete this.
1:57
S…
Speaker 1 (10- Re-throwing Exceptions)
Now we put the carrot over here,
1:59
S…
Speaker 1 (10- Re-throwing Exceptions)
alt and enter.
2:01
S…
Speaker 1 (10- Re-throwing Exceptions)
So far we use this command,
2:02
S…
Speaker 1 (10- Re-throwing Exceptions)
surround with try -catch,
2:04
S…
Speaker 1 (10- Re-throwing Exceptions)
we can use add exception to method signature.
2:06
S…
Speaker 1 (10- Re-throwing Exceptions)
There you go.
2:07
S…
Speaker 2 (10- Re-throwing Exceptions)
Now,
2:08
S…
Speaker 1 (10- Re-throwing Exceptions)
in our main method,
2:10
S…
Speaker 1 (10- Re-throwing Exceptions)
we need to cache this exception.
2:12
S…
Speaker 1 (10- Re-throwing Exceptions)
So let's wrap it with a try -catch block,
2:15
S…
Speaker 1 (10- Re-throwing Exceptions)
and this is where we can print a generic error message like an
2:20
S…
Speaker 1 (10- Re-throwing Exceptions)
unexpected error occur.
2:23
S…
Speaker 1 (10- Re-throwing Exceptions)
like desktop or mobile applications,
2:26
S…
Speaker 1 (10- Re-throwing Exceptions)
you have a generic exception handler that catches all kinds of exceptions
2:30
S…
Speaker 1 (10- Re-throwing Exceptions)
and displays a generic error message.
2:32
S…
Speaker 1 (10- Re-throwing Exceptions)
Now here we're catching an IO exception,
2:35
S…
Speaker 1 (10- Re-throwing Exceptions)
we can make this more general by catching exception objects or
2:39
S…
Speaker 1 (10- Re-throwing Exceptions)
throwable objects.
2:41
S…
Speaker 1 (10- Re-throwing Exceptions)
So earlier I told you that the throwable class is the parent
2:45
S…
Speaker 1 (10- Re-throwing Exceptions)
of all exceptions and errors in Java applications.
2:49
S…
Speaker 1 (10- Re-throwing Exceptions)
So with this,
2:49
S…
Speaker 1 (10- Re-throwing Exceptions)
we'll make sure that no matter what kind of error or exception we get,
2:53
S…
Speaker 1 (10- Re-throwing Exceptions)
our program displays a generic error message to the user.
2:56
S…
Speaker 1 (10- Re-throwing Exceptions)
Now let's run.
2:58
S…
Speaker 2 (10- Re-throwing Exceptions)
So,
3:00
S…
Speaker 1 (10- Re-throwing Exceptions)
we logged the exception and printed a friendly message to the user.
תעתיק זה נוצר על ידי AI (זיהוי דיבור אוטומטי). עשוי להכיל שגיאות □ אימות מול השמע המקורי לשימוש קריטי. מדיניות AI
תקציר
לחץ לסכם כדי ליצור סיכום AI של תעתיק זה.
מסכם...
שאל את אל על התעתיק הזה.
שאל כל דבר על התמליל הזה, הבינה המלאכותית תמצא חלקים רלוונטיים ותענה.