Throw Vs Throws | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Throw Vs Throws

What is the difference between throw and throw, and when should I use them ?

24th Jun 2022, 2:30 PM
Mohamed
Mohamed - avatar
4 Answers
+ 2
The throws keyword is used in a method signature and declares which exceptions can be thrown from all method. The throws keyword can be useful for propagating exceptions in the call stack and allows exceptions to not necessarily be handled within the method that declares these exceptions. the throw keyword is used within a method body, or any block of code, and is used to explicitly throw a single exception. The throw keyword can be useful for throwing exceptions based on certain conditions within a code block and for throwing custom exceptions.
26th Jun 2022, 6:38 AM
😇🌟SWATI🌟😇
😇🌟SWATI🌟😇 - avatar
+ 3
Mohamed , please complete the required information about programming language in the tags. thanks!
24th Jun 2022, 2:38 PM
Lothar
Lothar - avatar
+ 3
Throws is to declare that a specific method might throw some sort of exception, throw declares the specific exception in block inside method (exception can be of any type user defined,or predefined)
24th Jun 2022, 2:39 PM
Satyam Mishra
Satyam Mishra - avatar
+ 2
throw is for break execution programmatically if (bad) throw new Exception(); throws say to "others" this method can do throw void myMethod() throws Exception { if (bad) throw new Exception(); and they have to catch and solve it by try - catch command because this method don't care
25th Jun 2022, 1:04 AM
zemiak