• 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

Throws and throw clause

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to know the difference between
Throws and Throw clause in exception handling

Thanks
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
15 seconds on Google returned this:

http://forum.java.sun.com/thread.jspa?threadID=508335

Jeremy
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java methods are crazy. Nutty. Intolerable sometimes.

When something bad happens in a program, like perhaps a user asks for an element of an array that doesn't exist, a method takes a hissy fit and will throw an exception. When crazy things happen, methods will throw, no ess (s) at the end, an exception. And there are all sorts of types of exceptions they can throw. In the array case, it would be an ArrayOutOfBoundsException.

Now, methods that are crazy must label themsevles as such. If you're going to work with crazy people, it's nice to know they're unstable. Any method that is nutty and will throw an exception must state, EXPLICITLY in the method declaration, the exceptions that it throwS.

public void myMethod() throws ExamScamException {

if(true) {throw new ExamScamException("Told ya I was crazy");}

}

There are exceptions. Certain runtime exceptions that can just happen don't have to be listed in the throws clause, but for the most part, if a method is nutty, it's gotta tell the world how it is nutty by listing the exceptions it throws.
 
Amit Mohan Tiwari
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi kameron

i am asking straight forward answer not any rounded answer

please give me straight forward answer so that i can understand it
i am not getting your answer

Thanks
 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when a method triggers an exception, it will throw one

throw new ExamScamException("Problem");

Any method that does throw an exception must use the throws keyword in the method declaration, so anyone calling the method knows about the instability:

public void buyMyBooks() throws ExamScamException{

}

That's the difference. That's how they're used.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic