מציג בלבד
0:03
S… Speaker 1 (10- Handling Exceptions)
You'll learn how to consume the result of an asynchronous task.
0:07
S… Speaker 1 (10- Handling Exceptions)
But what if this task throws an exception?
0:09
S… Speaker 1 (10- Handling Exceptions)
For example,
0:10
S… Speaker 1 (10- Handling Exceptions)
let's say we try to call a remote weather service to get the current weather in
0:14
S… Speaker 1 (10- Handling Exceptions)
a given city.
0:15
S… Speaker 1 (10- Handling Exceptions)
Now this remote call may fail for various reasons.
0:18
S… Speaker 1 (10- Handling Exceptions)
So let's see how we can handle exceptions and recover by transforming
0:22
S… Speaker 1 (10- Handling Exceptions)
an exception into a value.
0:23
S… Speaker 1 (10- Handling Exceptions)
So I'm going to create a completable future object
0:27
S… Speaker 1 (10- Handling Exceptions)
using the supply async method in the body
0:32
S… Speaker 1 (10- Handling Exceptions)
of this lambda
0:33
S… Speaker 1 (10- Handling Exceptions)
First we print a message like getting the current weather and
0:38
S… Speaker 1 (10- Handling Exceptions)
then we throw a new Illegal state
0:43
S… Speaker 1 (10- Handling Exceptions)
exception the type of the exception doesn't matter.
0:46
S… Speaker 1 (10- Handling Exceptions)
I just want to throw an exception now this returns a
0:50
S… Speaker 1 (10- Handling Exceptions)
completable future So let's run our program and see what happens We
0:55
S… Speaker 1 (10- Handling Exceptions)
didn't see anything because this exception
0:59
S… Speaker 1 (10- Handling Exceptions)
was thrown on a different thread to get this exception We have to call the
1:03
S… Speaker 1 (10- Handling Exceptions)
get method of the future interface
1:05
S… Speaker 1 (10- Handling Exceptions)
So if you look at the documentation of the future interface,
1:08
S… Speaker 1 (10- Handling Exceptions)
you can see that the get method returns a value of type v and
1:12
S… Speaker 1 (10- Handling Exceptions)
it may throw an exception of type interrupted exception or execution
1:16
S… Speaker 1 (10- Handling Exceptions)
exception.
1:17
S… Speaker 1 (10- Handling Exceptions)
So if an exception is thrown on the thread that is executing our task,
1:21
S… Speaker 1 (10- Handling Exceptions)
the get method is able to propagate that exception and bring it into
1:25
S… Speaker 1 (10- Handling Exceptions)
our main thread.
1:26
S… Speaker 1 (10- Handling Exceptions)
Let me show you.
1:26
S… Speaker 1 (10- Handling Exceptions)
So here we call future
1:30
S… Speaker 1 (10- Handling Exceptions)
.get and then wrap this inside a try catch
1:34
S… Speaker 1 (10- Handling Exceptions)
block
1:36
S… Speaker 1 (10- Handling Exceptions)
then run our program,
1:37
S… Speaker 1 (10- Handling Exceptions)
now we get the exception.
1:39
S… Speaker 1 (10- Handling Exceptions)
So you are trying to get the current weather,
1:42
S… Speaker 1 (10- Handling Exceptions)
but we got an execution exception.
1:45
S… Speaker 1 (10- Handling Exceptions)
This execution exception is caused by an illegal state
1:49
S… Speaker 1 (10- Handling Exceptions)
exception.
1:50
S… Speaker 1 (10- Handling Exceptions)
So the exception that we threw in our lambda is wrapped inside
1:54
S… Speaker 1 (10- Handling Exceptions)
an execution exception.
1:56
S… Speaker 1 (10- Handling Exceptions)
So when we call the get method,
1:58
S… Speaker 1 (10- Handling Exceptions)
you may get an interrupted exception.
2:01
S… Speaker 1 (10- Handling Exceptions)
This happens if the thread is sleeping,
2:02
S… Speaker 1 (10- Handling Exceptions)
but we try to interrupt it.
2:04
S… Speaker 1 (10- Handling Exceptions)
Or we may get an execution exception.
2:07
S… Speaker 1 (10- Handling Exceptions)
This happens if something goes wrong during the execution of our asynchronous
2:11
S… Speaker 1 (10- Handling Exceptions)
task.
2:12
S… Speaker 1 (10- Handling Exceptions)
Now we can get the actual cause of this exception by calling e
2:16
S… Speaker 1 (10- Handling Exceptions)
.get cause.
2:17
S… Speaker 1 (10- Handling Exceptions)
This will return the exception that we threw here.
2:21
S… Speaker 2 (10- Handling Exceptions)
Now,
2:23
S… Speaker 1 (10- Handling Exceptions)
we don't want our application to crash.
2:25
S… Speaker 1 (10- Handling Exceptions)
So what should we do here?
2:26
S… Speaker 2 (10- Handling Exceptions)
Well,
2:27
S… Speaker 1 (10- Handling Exceptions)
in this case we can recover and provide a default value
2:31
S… Speaker 1 (10- Handling Exceptions)
like the last temperature that we successfully read before.
2:35
S… Speaker 1 (10- Handling Exceptions)
Let me show you how to do that.
2:36
S… Speaker 1 (10- Handling Exceptions)
So first I'm going to delete this line.
2:38
S… Speaker 1 (10- Handling Exceptions)
Before we call the get method,
2:41
S… Speaker 1 (10- Handling Exceptions)
we call another method called exceptionally.
2:45
S… Speaker 1 (10- Handling Exceptions)
Now look at the signature of this method.
2:47
S… Speaker 1 (10- Handling Exceptions)
It takes a function that maps a throwable to a different
2:51
S… Speaker 1 (10- Handling Exceptions)
type.
2:52
S… Speaker 1 (10- Handling Exceptions)
We talked about throwables earlier in the section about exceptions.
2:55
S… Speaker 1 (10- Handling Exceptions)
So this throwable class is the base or parent
3:00
S… Speaker 1 (10- Handling Exceptions)
of all errors and exceptions in Java.
3:03
S… Speaker 1 (10- Handling Exceptions)
So here we need to pass a lambda that maps this exception to a different type.
3:07
S… Speaker 1 (10- Handling Exceptions)
So we can get this exception and map it to a numeric value like
3:11
S… Speaker 1 (10- Handling Exceptions)
1.
3:11
S… Speaker 1 (10- Handling Exceptions)
Let's imagine this is the last temperature that we read successfully.
3:15
S… Speaker 2 (10- Handling Exceptions)
Now,
3:16
S… Speaker 1 (10- Handling Exceptions)
when we call the get method,
3:17
S… Speaker 1 (10- Handling Exceptions)
we get the result.
3:19
S… Speaker 1 (10- Handling Exceptions)
That is the last temperature.
3:20
S… Speaker 1 (10- Handling Exceptions)
So let's store it over here and then print it.
3:24
S… Speaker 2 (10- Handling Exceptions)
We didn't get
3:28
S… Speaker 1 (10- Handling Exceptions)
an exception this time.
3:29
S… Speaker 1 (10- Handling Exceptions)
So this is the benefit of using the exceptionally method.
3:32
S… Speaker 2 (10- Handling Exceptions)
Now,
3:33
S… Speaker 1 (10- Handling Exceptions)
one thing you need to understand is that this method will return a new completable
3:37
S… Speaker 1 (10- Handling Exceptions)
future.
3:38
S… Speaker 1 (10- Handling Exceptions)
So this completable future is different from the one we originally
3:42
S… Speaker 1 (10- Handling Exceptions)
created.
3:42
S… Speaker 1 (10- Handling Exceptions)
With this method,
3:44
S… Speaker 1 (10- Handling Exceptions)
we're programming this completable future to return a default value if
3:48
S… Speaker 1 (10- Handling Exceptions)
an exception is thrown.
3:49
S… Speaker 1 (10- Handling Exceptions)
So when we call the get method on this new completable future,
3:53
S… Speaker 1 (10- Handling Exceptions)
we'll get that default value.
3:54
S… Speaker 1 (10- Handling Exceptions)
If you call the get method on the original completable future,
3:58
S… Speaker 1 (10- Handling Exceptions)
we'll get an exception.
3:59
S… Speaker 1 (10- Handling Exceptions)
So this is something that you're going to see again and again throughout this section.
4:03
S… Speaker 1 (10- Handling Exceptions)
A lot of methods in the completable future class return a new
4:07
S… Speaker 1 (10- Handling Exceptions)
completable future.
4:08
S… Speaker 1 (10- Handling Exceptions)
With this we can build a recipe for our asynchronous operations.

תעתיק זה נוצר על ידי AI (הכרה אוטומטית של דיבור). ייתכן שיש בו שגיאות אשר מאשרות את השמע המקורי לשימוש קריטי. מדיניות AI

❤️ אוהבים את STT.ai? ספרו לחברים שלכם!
תקציר
לחץ לסכם כדי ליצור סיכום AI של תעתיק זה.
מסכם...
שאל את אל על התעתיק הזה.
שאל כל דבר על התמליל הזה, הבינה המלאכותית תמצא חלקים רלוונטיים ותענה.