This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic