what is the difference between throw & throws ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

what is the difference between throw & throws ?

why is these two used in Java am still confused.. !!

16th Sep 2016, 2:30 PM
Jithin Sathianandan
Jithin Sathianandan - avatar
3 Answers
+ 4
I rarely use these because i mostly handle exceptions with try, catch , finally blocks . However from my experience i seem to use them in methods that could throw an exception error. So basically you use the "throws" keyword after the parenthesis of the method name , which eventually says something along the line of " this method COULD throw an exception". Then you actually use the "throw" keyword followed by the instantiation of that particular exception class in the section where you want to throw the exception. Finally to actually understand the entire concept in detail i recommend you to read the java reference page. Hopefully i have helped!
16th Sep 2016, 4:14 PM
Ousmane Diaw
+ 4
to manualy throw the exception use the "throw" and the method that capable of throwing an exception is must be specified by "throws" and when it is not specified then compile time errors will be the result
18th Sep 2016, 6:26 AM
kunal patil
kunal patil - avatar
0
You are writing a method which can cause exception ( Not always the case, only happens in certain programming conditions), then you must mention it in the method signature with ...method.. throws "exception type" sothat when you or someone else want to use the method in their code, before hand they know by warning by compiler that there could be some exception, should be handled. And throw is used by programmer to manually throw an exception. He goes to temple ( Everyday it may not be the case, today he may not be going) but go to the temple ( an order, he has to go)
2nd Nov 2016, 2:53 AM
Siva Prasad
Siva Prasad - avatar