+ 1
Throw Vs Throws
What is the difference between throw and throw, and when should I use them ?
4 Antworten
+ 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.
+ 3
Mohamed ,
please complete the required information about programming language in the tags.
thanks!
+ 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)
+ 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