Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes Throws and throw Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Throws and throw" Watch "Throws and throw" New topic

Throws and throw

Ranch Hand

Joined: Jan 24, 2006
Posts: 54
difference between throws ,throw and throwable
Chetan Raju
Ranch Hand

Joined: Aug 02, 2006
Posts: 109
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
Ranch Hand

Joined: Jan 24, 2006
Posts: 54
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

Joined: Nov 08, 2006
Posts: 52

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.
Ranch Hand

Joined: Jan 24, 2006
Posts: 54
Thanks David !
Mattias Ahlin

Joined: May 31, 2006
Posts: 11
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 ]

Best regards,<br />Mattias
I agree. Here's the link:
subject: Throws and throw
It's not a secret anymore!