• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

throw & throws

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
can some one tell me the diff between throw and throws with a an example
Thanks
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
throw and throws are two different things. the throw statement occurs inside a block e.g. a method body. To throw an exception, you would use
void aMethod() {
throw new Exception("new exception");
}
the throws clause, appears as a method header,
e.g.
void bMethod() throws FileNotFoundException {}
A method that can throw an exception, doesn't need to specify it in it's throws clause.
if you use a try catch block to call a method that throws an exception, that's fine. otherwise if you don't catch the exception, you must provide the throws clause in the header of the calling method.
example.......

in order to call this method you can use either

or....

hope that clarifys it!
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add, a checked exception must be either declared in the throws clause of the method, or placed inside a try-catch block. A method does not have to declare a runtime exception in its throws clause, nor catch it.
 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amir,
When you throw an exception using throw inside any method ,you should also declare it in methods header using throws clause.
So the following code doesn't compile.
void aMethod() {
throw new Exception("new exception");
}
Correct syntax would be
void aMethod() throws Exception {
throw new Exception("new exception");
}
Ususally you use throw statement with conditional statement.
For example

You are advertising every possible exception that your method might actually throw by declaring them in methods header using throws clause.
Hope that helps.
Veena
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic