A practical and simple example will be very helpful.When already try-catch block and throws keyword available ,is it worthy throwing an exception object manually? try-catch block helps in catching an exception and displaying the information about that exception during Runtime right? Can also anyone could tell the difference between "throw" and "throws" keyword?
Asked
Active
Viewed 141 times
0
-
So you can "throw" something, unlike "return" something, it says, "throw this exception as something has gone wrong", where are "throws" says, "I might throw one or more exceptions, so you should be prepared" – MadProgrammer Feb 09 '17 at 04:01
-
The Online Java Tutorial on exceptions (https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html) has lots of examples. – Dawood ibn Kareem Feb 09 '17 at 04:02
-
@MadProgrammer Why not wrte an answer? :) – synchronizer Feb 09 '17 at 04:02
-
For your **custom throwables**. You may wany to throw it for other components of your application to capture and handle the error. – Siddharth Tyagi Feb 09 '17 at 04:02
-
2@synchronizer Because, to be frank, I shouldn't have to, there are so many tutorials on the subject it's ridiculous :P – MadProgrammer Feb 09 '17 at 04:03
-
Yeah, we SO need to bring back the GR close reason. – Dawood ibn Kareem Feb 09 '17 at 04:17
1 Answers
0
The meaning is there in the words only.
Keyword throw
is a action word, for example throw new Exception()
means the code is throwing an Exception, meaning if the execution control reaches this code, exception will be thrown. This is internal to a method.
Keyword throws
is a declaration to the users of the method (code calling the method) that the method can throw an exception.
try-catch
is an mechanism to try a code which can throw an exception and then catch the same if thrown.

nits.kk
- 5,204
- 4
- 33
- 55