Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Throws and throw clause

 
Amit Mohan Tiwari
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to know the difference between
Throws and Throw clause in exception handling

Thanks
 
Jeremy Botha
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
15 seconds on Google returned this:

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

Jeremy
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic