• Post Reply Bookmark Topic Watch Topic
  • New Topic

exceptions difference between throw and throws  RSS feed

 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are the difference between this ?there is a difference or is the same things?
 
Mauro Blanc
Greenhorn
Posts: 1
Android Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Throws clause in used to declare an exception and throw keyword is used to throw an exception explicitly.
The keyword throw is used inside method body to invoke an exception and throws clause is used in method declaration (signature).

 
Shaun Wild
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
throw

the throw keyword in java allows you to throw an exception, this will cause a program to terminate unless it is caught in a try- catch statement.

throws

the throws keyword in java allows you to say "this method may throw this exception", a user calling this method will be required to place it in a try-catch statement an example of this:



This method uses both of the keywords, it will throw an Exception if result < 0 and it also tells the user that the method can throw an IllegalArgumentException.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!