Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between throws and throw keyword.  RSS feed

 
Manaf Abdul
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell me the difference between throw and throws keyword alongwith appropriate examples.
 
Dóra Takács
Ranch Hand
Posts: 39
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone tell me the difference between throw and throws keyword alongwith appropriate examples.


1.)----------------------------------------------------------------------------------------------
The throws keyword:

The throws keyword is used in method declarations. It basically means that when you write the method you shout out "Hey, I will use something in this method that might cause an exception of this type! Be careful! Handle it or pass it on!"


2.)----------------------------------------------------------------------------------------------
The throw keyword:

Here, the throw says that something actually went wrong. You know for sure, that something happened that should not have happened, and you anticipated the possibility. In this case, without exception handling, the execution of the method would end here, and unless the programmer invoking your method handled the possible exception, the program will end with a stack trace of the exception, so you would see what happened, what went wrong.



Summary) -------------------------------------------------------------------------------------
The point is, with throws, you declare that something might go wrong in your method.
With throw, you define what kind of problem occoured, and you know for sure that something did indeed, go wrong during execution.
Also, as you see in the example, the throw keyword requires you to have an exception object, which you might aswell create on the same line.


Best regards, Dóri
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dóra Takács wrote:The throws keyword is used in method declarations...

Excellent explanation. Have a cow.

I particularly like your inventive use of colour too - although I wouldn't go too overboard with it...

Winston
 
khadar valli
Greenhorn
Posts: 26
Java MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abdul,

These are some basic differences between throw and throws keywords.

1.Throw clause in used to declare an exception and throws keyword is used to throw an exception explicitly to its Calling method or Super Class.
2. If we see syntax wise than throw is followed by an instance variable and throws is followed by exception class names.
3. The keyword throw is used inside method body to invoke an exception and throws clause is used in method declaration (signature).
4. To throw an exception we use throw keyword while to handle (catch) an exception we use throws clause.
5. Actually Throws keyword is used to throw the checked exceptions which a programmer doesn’t want to handle it so a programmer throws these exception so that compiler did not give an error but throw keyword is used to throw an built-in-exception or user defined exception explicitly.

Regards,
Khadarvalli.
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's all in the Java™ Tutorials.
 
Manaf Abdul
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dóra Takács wrote:
Can someone tell me the difference between throw and throws keyword alongwith appropriate examples.



2.)----------------------------------------------------------------------------------------------
The throw keyword:

Here, the throw says that something actually went wrong. You know for sure, that something happened that should not have happened, and you anticipated the possibility. In this case, without exception handling, the execution of the method would end here, and unless the programmer invoking your method handled the possible exception, the program will end with a stack trace of the exception, so you would see what happened, what went wrong.



Summary) -------------------------------------------------------------------------------------
The point is, with throws, you declare that something might go wrong in your method.
With throw, you define what kind of problem occoured, and you know for sure that something did indeed, go wrong during execution.
Also, as you see in the example, the throw keyword requires you to have an exception object, which you might aswell create on the same line.


Best regards, Dóri


Great explanation indeed but I still dont understand that why do you need to unnecessarily throw an exception using "throw"
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
khadar valli wrote:. . .
1.Throw clause in used to declare an exception and throws keyword is used to throw an exception explicitly to its Calling method or Super Class.
. . .
4. To throw an exception we use throw keyword while to handle (catch) an exception we use throws clause.
. . .
I am afraid those answers you made are not clear. I suspect they are incorrect, but I am not sure.
 
Dóra Takács
Ranch Hand
Posts: 39
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still dont understand that why do you need to unnecessarily throw an exception using "throw"


By using throws, executionwise, you do nothing. You just say that something might happen. You have to use throw, to actually modify the executionpath, thus affecting the running program.

But lets see a scenario where you want to open a file on your computer, for example, an image of a cat. You load it into memory, and put a reference to it like you would with any object.
But what if there were no image to load, because lets say, someone deleted it? Your reference would be null, as there is no object created behind it, and you might later call methods on it.
Then, to prevent this happening, in your code, you could say:

This way the caller of your openImage() method will know that he gave the method an invalid path to the image, or should go and ask his big brother why he deleted his favourite cat image.
 
Dóra Takács
Ranch Hand
Posts: 39
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I particularly like your inventive use of colour too - although I wouldn't go too overboard with it...

Thank you! I just wanted to somehow differentiate between the very similar looking keywords :3
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!