what is difference between throw and throws | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

what is difference between throw and throws

16th Dec 2016, 7:20 AM
Eswari.M
Eswari.M - avatar
2 ответов
0
Throws is used to declare an exception and throw is used to throw an exception explicitly. In other words, generally Checked exeptions we use throws and unchecked exeptions throw.
16th Dec 2016, 8:55 AM
Felipe Cruz
Felipe Cruz - avatar
0
Hmm, I think Felipe's answer is not the full story: throws is used to specify Checked exceptions on function signatures, like this: public void foo() throws(MyExcept1, MgExcept2) { ... throw MyExcept1("some msg") ; You then use throw inside the method body to throw these declared exceptions. You don't normally throw unchecked exceptions yourself - these are JVM (or OS) generated when bad things happen (like divide by 0/out of memory /etc)
16th Dec 2016, 9:07 AM
Ettienne Gilbert
Ettienne Gilbert - avatar