Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Throws and throw

 
Confused
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
difference between throws ,throw and throwable
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
difference between throws ,throw and throwable


A method THROWS exception(s)
You throw an exception explicitly using THROW
Throwable is a super class of all exceptions / errors
 
Confused
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks chetan.
but tell me one thing,
suppose i have a method called calculation() and that method is in a
try/ catch block and if any error occurs within that method then catch will show me the error like ArithmethicException ...something like that
and it is an example of throw ... which is happening implicitly ...
am I right ?

and when I want any of my method will throw me a particuler exception that time I can invoke a method like this

double calculation throws ArithmethicException()
{
}
am I right ...
 
David Nemeskey
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piyali:

Yes, basically:



Of course, the code above is a bit silly; Throwables include Errors as well, not just Exceptions, so it may not be useful to catch them. Also, ArithmeticException is a runtime exception (descendant of RuntimeException), so it is not required to declare it in the method header. But doing so is a good idea, because the user of your API will get a better overview of what's happening.
 
Confused
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David !
:-)
 
Mattias Ahlin
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But normally you don't declare your RuntimeExceptions in the method header. Instead you declare them in the method's javadoc. See java.util.Vector for instance:


Otherwise your API will be polluted with RuntimeExceptions that the clients of the API normally shouldn't be catching anyway.
[ November 28, 2006: Message edited by: Mattias Ahlin ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic