#80 Exception throw keyword in Java
HTML-код
- Опубликовано: 10 янв 2025
- Check out our courses:
Java Full Stack and Spring AI - go.telusko.com...
Coupon: TELUSKO10 (10% Discount)
DevOps with AWS: From Basics to Mastery : go.telusko.com...
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : go.telusko.com...
Coupon: TELUSKO20 (20% Discount)
For More Queries WhatsApp or Call on : +919008963671
Udemy Courses:
Spring: go.telusko.com...
Java:- go.telusko.com...
Java Spring:- go.telusko.com...
Java For Programmers:- go.telusko.com...
Python : go.telusko.com...
Git : go.telusko.com...
Docker : go.telusko.com...
website : courses.telusk...
In this lecture we are discussing throw keyword :
#1
-- throw keyword in Java is used to explicitly throw an exception.
When an exception is thrown using the throw keyword, the execution of the current method is stopped.
syntax-
throw new NullPointerException("Object is null");
e.g
public void divide(int a, int b) {
if (b == 0) {
throw new ArithmeticException("Cannot divide by zero");
}
int result = a / b;
System.out.println(result);
}
#2
-- throw keyword is used to throw exceptions, not to catch them. To catch exceptions, you need to use a try-catch block.
-- if you do not use try-catch then the control is passed, where the method is called.
-- when you want to pass message to an exception
then you should use the parametrized constructor instead non parameterized constructor
e.g
class Main{
public static void main(String []args){
int a=0;
try{
if(a==0)
throw new ArithmeticException("a should not be zero"); // throw new ArithmeticException() is non parametrized constructor
}
catch(ArithmeticException e){
System.out.println("Exception caught: "+e);
}
}
}
Output: Exception caught: java.lang.ArithmeticException: a should not be zero
class Main{
public static void main(String[] args){
int j=30;
int i=1;
try{
j=18/i;
//handle the exception using throw
if(j==0)
throw new ArithmeticException("i donot want to print 0"); //try to create an exception
}
catch(ArithmeticException e){//here catch is catching the exception
System.out.println("ArithmeticException caught");
System.out.println(e.getMessage()); //getMessage() method use to get the message
}
catch(Exception e){
System.out.println("Exception caught");
}
}
}
Github repo : github.com/nav...
Java:- bit.ly/JavaUde...
Spring:- bit.ly/SpringU...
More Learning :
Java :- bit.ly/3x6rr0N
Python :- bit.ly/3GRc7JX
Django :- bit.ly/3MmoJK6
JavaScript :- bit.ly/3tiAlHo
Node JS :- bit.ly/3GT4liq
Rest Api :-bit.ly/3MjhZwt
Servlet :- bit.ly/3Q7eA7k
Spring Framework :- bit.ly/3xi7buh
Design Patterns in Java :- bit.ly/3MocXiq
Docker :- bit.ly/3xjWzLA
Blockchain Tutorial :- bit.ly/3NSbOkc
Corda Tutorial:- bit.ly/3thbUKa
Hyperledger Fabric :- bit.ly/38RZCRB
NoSQL Tutorial :- bit.ly/3aJpRuc
Mysql Tutorial :- bit.ly/3thpr4L
Data Structures using Java :- bit.ly/3MuJa7S
Git Tutorial :- bit.ly/3NXyCPu
Donation:
PayPal Id : navinreddy20
www.telusko.com
Something different from your tutorials compared to others is, it is very engaging. Never feels bored even for a second. Thankyou so much Navin!!
feel like im on ther home run of this tutorial. not quite done yet but thank you so much for this content. its crazy that in this world you can learn to code without even player a doller.... thanks telusko. you're a legend.
at 4:02 the exception is caused by the 9th line and the throw exception command will not execute since it will directly jump to catch with the same ArithmeticException. instead you could have move lines 10 and 11 to 9 and 9 to 11. but you have cleared the concept.
Thankyou for the explanation sir. Following your java lectures.... enjoying it♥️🙂🙂♥️
Thank you...after longtime I got to know the real use of throw keyword.
@4:54 if i's value 0 then output : it shows /zero.
and if i's value 20 then output: I don't want to print zero.
The best java course ever😍
Sir Your video is very help full.
Thank You
Superb explanation 👍
Amazing explanation
hello sir,
on executing line 9 itself, error occurs and the execution enters the catch block rite ? Line number 10 won't be executed at all and hence the 'throw' code. Am i getting it rite ?
Yes you are right
aren't u from telangana sir!
That’s why the channel name is telusko which means (learn) in Telugu
Hey, what does the throws keyword do?
Throws keyword indicates that there might be error in this block of code
Thank you sir❤❤
I have one doubt, how ArithmeticException object becomes constructor?
Every time we create object using new className()
the constructor of that class is invoked depending on parameters we pass, he meant that
God bless you
amazing !
Thank you
There is an error in the if part it is if(i==0) but u placed if(j==0). As the denominator should be checked that is i
Nope buddy, If you'll re-watch the video, he explained that he doesn't want the value of J to be zero( which comes as an output when 18 is divided by 20).
Since the above division operation is not an exception, catch block is not called, that's why we are using the keyword "Throw" to make this operation an exception and catched by catch block.
The code in the video is totally correct 🙂
@@ananysharma9290 ok dude tqs
don pollo