#80 Exception throw keyword in Java
HTML-код
- Опубликовано: 3 авг 2024
- Check out our courses:
Spring and Microservices Weekend Live Batch : bit.ly/spring-live-weekend
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : bit.ly/java-spring-cloud
Udemy Courses:
Java:- bit.ly/JavaUdemyTelusko
Spring:- bit.ly/SpringUdemyTelusko
Java For Programmers:- bit.ly/javaProgrammers
For More Queries WhatsApp or Call on : +919008963671
website : courses.telusko.com/
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/navinreddy20/Javac...
Java:- bit.ly/JavaUdemyTelusko
Spring:- bit.ly/SpringUdemyTelusko
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!!
Thankyou for the explanation sir. Following your java lectures.... enjoying it♥️🙂🙂♥️
The best java course ever😍
Superb explanation 👍
Sir Your video is very help full.
Thank You
Amazing explanation
Thank you sir❤❤
God bless you
amazing !
Thank you
Hey, what does the throws keyword do?
Throws keyword indicates that there might be error in this block of code
aren't u from telangana sir!
That’s why the channel name is telusko which means (learn) in Telugu
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
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
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