Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

throws and throw in exception

 
Tulsi Rai
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I know that if a method is declaring to it's users it's throwing exceptions by using "throws" keyword. but i have seen that at the same method that has throws keywork, also has throw new someexception() inside the method. what's this throw means there? can you please explain it?
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The throws clause lists all checked exceptions that can be thrown in a method.

A throw statement creates an exception object. Execution continues at a matching catch block or terminates if none is found. The exception object is passed to the catch block.

In short, throws is a specification and throw is an executable statement.
 
Tony Morris
Ranch Hand
Posts: 1608
 
lakshmipathi Gandhi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exceptions are thrown by throw statement regardless of who throws the exception. we use the throw statement inside the method to throw an excetion

throws clause tells the code which r calling the method should handle the exception.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
trai,

Welcome (back) to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic