9- Throwing Exceptions
Jun 18, 2026 04:37
· 4:41
· English
· Whisper Turbo
· 2 स्पीकर
इस हस्तलिपि का समय ख़त्म हो गया. 21 दिन.
स्थायी भंडारण के लिए अद्यतन करें →
सिर्फ दिखाएँ
0:03
S…
Speaker 2 (9- Throwing Exceptions)
So far we've only been handling or catching exceptions,
0:06
S…
Speaker 2 (9- Throwing Exceptions)
but sometimes we want to throw an exception.
0:09
S…
Speaker 1 (9- Throwing Exceptions)
Let me show you an example.
0:10
S…
Speaker 2 (9- Throwing Exceptions)
So in this package,
0:11
S…
Speaker 2 (9- Throwing Exceptions)
the exceptions package,
0:13
S…
Speaker 2 (9- Throwing Exceptions)
I'm going to add a new class called account.
0:16
S…
Speaker 2 (9- Throwing Exceptions)
Here we need a method for depositing
0:21
S…
Speaker 2 (9- Throwing Exceptions)
money into this account.
0:22
S…
Speaker 1 (9- Throwing Exceptions)
So public void deposit,
0:25
S…
Speaker 2 (9- Throwing Exceptions)
we give it a parameter of type float called value.
0:30
S…
Speaker 2 (9- Throwing Exceptions)
Now this value has to be a positive value.
0:32
S…
Speaker 2 (9- Throwing Exceptions)
It doesn't make sense to pass 0 or negative 1 here.
0:35
S…
Speaker 2 (9- Throwing Exceptions)
So we can validate this argument and throw an exception.
0:38
S…
Speaker 2 (9- Throwing Exceptions)
If value is less than or equal to 0,
0:42
S…
Speaker 1 (9- Throwing Exceptions)
we throw,
0:44
S…
Speaker 2 (9- Throwing Exceptions)
so we type the throw keyword,
0:45
S…
Speaker 2 (9- Throwing Exceptions)
then we create an exception object.
0:48
S…
Speaker 2 (9- Throwing Exceptions)
In this case,
0:49
S…
Speaker 2 (9- Throwing Exceptions)
new illegal argument exception.
0:52
S…
Speaker 2 (9- Throwing Exceptions)
We're telling the caller of this method that they have passed an illegal argument.
0:56
S…
Speaker 1 (9- Throwing Exceptions)
Now,
0:57
S…
Speaker 2 (9- Throwing Exceptions)
back to our demo class.
1:00
S…
Speaker 2 (9- Throwing Exceptions)
Let's create an account object and then
1:04
S…
Speaker 2 (9- Throwing Exceptions)
call account .deposit with negative 1.
1:07
S…
Speaker 2 (9- Throwing Exceptions)
When we run this program,
1:09
S…
Speaker 2 (9- Throwing Exceptions)
our program crashes because we have an illegal
1:13
S…
Speaker 2 (9- Throwing Exceptions)
argument exception.
1:14
S…
Speaker 2 (9- Throwing Exceptions)
This is a runtime or unchecked exception which indicates a
1:18
S…
Speaker 2 (9- Throwing Exceptions)
programming error.
1:18
S…
Speaker 2 (9- Throwing Exceptions)
So we shouldn't handle this with a try -catch block.
1:22
S…
Speaker 2 (9- Throwing Exceptions)
We should come back and fix the source of the problem.
1:24
S…
Speaker 2 (9- Throwing Exceptions)
So we pass a positive 1.
1:28
S…
Speaker 1 (9- Throwing Exceptions)
Now the exception is gone.
1:31
S…
Speaker 2 (9- Throwing Exceptions)
So this is how we can throw an exception in Java.
1:34
S…
Speaker 2 (9- Throwing Exceptions)
Now this technique that we have used here validating this argument and
1:38
S…
Speaker 2 (9- Throwing Exceptions)
throw an exception is called defensive programming.
1:41
S…
Speaker 2 (9- Throwing Exceptions)
So if a negative value here can cause a serious problem down the
1:45
S…
Speaker 2 (9- Throwing Exceptions)
road, we throw an exception and prevent the rest of our code from getting executed.
1:50
S…
Speaker 2 (9- Throwing Exceptions)
This is why this technique is called defensive programming because we're
1:54
S…
Speaker 2 (9- Throwing Exceptions)
coding in a defensive manner.
1:56
S…
Speaker 2 (9- Throwing Exceptions)
So our code is immediately telling that it cannot work with a negative value.
2:00
S…
Speaker 2 (9- Throwing Exceptions)
This is better than letting this negative value get some serious problem
2:04
S…
Speaker 2 (9- Throwing Exceptions)
down the road.
2:05
S…
Speaker 1 (9- Throwing Exceptions)
For example,
2:06
S…
Speaker 2 (9- Throwing Exceptions)
you may store it in a database and sometime in the future,
2:08
S…
Speaker 2 (9- Throwing Exceptions)
you read it and then something really weird happens in your application.
2:12
S…
Speaker 2 (9- Throwing Exceptions)
So this is the benefit of defensive programming.
2:15
S…
Speaker 1 (9- Throwing Exceptions)
Having said that,
2:16
S…
Speaker 1 (9- Throwing Exceptions)
as I always say,
2:17
S…
Speaker 2 (9- Throwing Exceptions)
too much of a good thing can be a bad thing.
2:20
S…
Speaker 2 (9- Throwing Exceptions)
Adding this validation logic and throwing exception adds a bit of noise
2:24
S…
Speaker 1 (9- Throwing Exceptions)
to our code.
2:24
S…
Speaker 2 (9- Throwing Exceptions)
It makes our code a bit longer and more verbose.
2:27
S…
Speaker 2 (9- Throwing Exceptions)
This is a very simple example,
2:29
S…
Speaker 2 (9- Throwing Exceptions)
but what if we had a method with three parameters?
2:32
S…
Speaker 2 (9- Throwing Exceptions)
With this approach,
2:33
S…
Speaker 2 (9- Throwing Exceptions)
the first six lines of this method would just be data validation logic.
2:36
S…
Speaker 2 (9- Throwing Exceptions)
So I would say it's better to use defensive programming if
2:40
S…
Speaker 2 (9- Throwing Exceptions)
you're building a library or a framework for others to use.
2:44
S…
Speaker 2 (9- Throwing Exceptions)
If you're building an application,
2:45
S…
Speaker 2 (9- Throwing Exceptions)
you should have a level of trust in your code.
2:48
S…
Speaker 2 (9- Throwing Exceptions)
Your methods should trust each other.
2:49
S…
Speaker 2 (9- Throwing Exceptions)
So don't pollute every single method with data validation logic.
2:54
S…
Speaker 2 (9- Throwing Exceptions)
Perform this kind of validation only when you receive input from the user or
2:58
S…
Speaker 2 (9- Throwing Exceptions)
external systems.
2:59
S…
Speaker 2 (9- Throwing Exceptions)
So at the boundary of your application,
3:01
S…
Speaker 2 (9- Throwing Exceptions)
but not within the application itself.
3:03
S…
Speaker 2 (9- Throwing Exceptions)
Now here we're throwing a runtime or an unchecked exception,
3:07
S…
Speaker 2 (9- Throwing Exceptions)
but what if you want to throw a checked exception?
3:10
S…
Speaker 1 (9- Throwing Exceptions)
Let me show you.
3:10
S…
Speaker 2 (9- Throwing Exceptions)
So I'm going to replace this with IO exception.
3:14
S…
Speaker 2 (9- Throwing Exceptions)
Now we have a compilation error.
3:17
S…
Speaker 2 (9- Throwing Exceptions)
Unhandled exception,
3:19
S…
Speaker 1 (9- Throwing Exceptions)
IO exception.
3:20
S…
Speaker 2 (9- Throwing Exceptions)
So the Java compiler is saying we haven't handled this exception.
3:24
S…
Speaker 2 (9- Throwing Exceptions)
Now, should we wrap this inside the try catch block?
3:27
S…
Speaker 1 (9- Throwing Exceptions)
No,
3:28
S…
Speaker 2 (9- Throwing Exceptions)
it doesn't make sense to throw this exception and handle it right here.
3:32
S…
Speaker 2 (9- Throwing Exceptions)
We want to throw this exception in this method and have the caller of
3:36
S…
Speaker 2 (9- Throwing Exceptions)
this method handle that exception.
3:37
S…
Speaker 2 (9- Throwing Exceptions)
Just like how we use the file reader class.
3:40
S…
Speaker 2 (9- Throwing Exceptions)
So the file reader class throws an exception that we have to explicitly handle.
3:44
S…
Speaker 1 (9- Throwing Exceptions)
So,
3:45
S…
Speaker 2 (9- Throwing Exceptions)
to get rid of this compilation error,
3:47
S…
Speaker 2 (9- Throwing Exceptions)
Over here in the declaration of this method,
3:50
S…
Speaker 2 (9- Throwing Exceptions)
we should specify that this method may throw an IO exception.
3:54
S…
Speaker 1 (9- Throwing Exceptions)
So we type throws,
3:56
S…
Speaker 2 (9- Throwing Exceptions)
note that this is throws,
3:57
S…
Speaker 2 (9- Throwing Exceptions)
but here we're using the throw keyword.
4:00
S…
Speaker 1 (9- Throwing Exceptions)
So throws IO exception.
4:02
S…
Speaker 2 (9- Throwing Exceptions)
We're basically telling the caller of this method that this method may
4:06
S…
Speaker 2 (9- Throwing Exceptions)
throw an IO exception.
4:08
S…
Speaker 2 (9- Throwing Exceptions)
So this is part of the API of this method.
4:11
S…
Speaker 2 (9- Throwing Exceptions)
API is short for Application Programming Interface.
4:15
S…
Speaker 2 (9- Throwing Exceptions)
So anyone who wants to use our account class should know that the deposit method
4:19
S…
Speaker 2 (9- Throwing Exceptions)
takes a float and may throw an exception.
4:22
S…
Speaker 2 (9- Throwing Exceptions)
These details represent the interface or the API of
4:26
S…
Speaker 1 (9- Throwing Exceptions)
this method.
4:26
S…
Speaker 1 (9- Throwing Exceptions)
Okay?
4:27
S…
Speaker 1 (9- Throwing Exceptions)
Now,
4:28
S…
Speaker 2 (9- Throwing Exceptions)
back to our demo class,
4:29
S…
Speaker 2 (9- Throwing Exceptions)
the Java compiler is saying that we haven't handled this exception.
4:32
S…
Speaker 2 (9- Throwing Exceptions)
So this is where we wrap our code inside the try catch
4:37
S…
Speaker 1 (9- Throwing Exceptions)
block.
4:37
S…
Speaker 1 (9- Throwing Exceptions)
There you go.
ऐसा हो कि मूल ऑडियो में ग़लतियाँ हों — कठिन उपयोग के लिए मूल ऑडियो के विरुद्ध जाँच करें । एआई नीति
सारांश
इस बुक का एआई सारांश बनाने के लिए सार बनाने के लिए सार को क्लिक करें.
साझा कर रहा है...
एआई के बारे में पूछें इस ट्रांसमिशन के बारे में
इस हस्तलिपि के बारे में कुछ सवाल पूछिए — एआई को ज़रूरी भागों और जवाब मिलेगा ।