aspose file tools*
The moose likes Java in General and the fly likes Throws and throw clause Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Throws and throw clause" Watch "Throws and throw clause" New topic
Author

Throws and throw clause

Amit Mohan Tiwari
Greenhorn

Joined: Aug 21, 2006
Posts: 10
i want to know the difference between
Throws and Throw clause in exception handling

Thanks
Jeremy Botha
Ranch Hand

Joined: Feb 16, 2005
Posts: 125
15 seconds on Google returned this:

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

Jeremy


McFinnigan? Never heard of him. Nobody here but us chickens...<br /> <br />SCJP for Java 1.4<br />SCJD for Java 5.0
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

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

Joined: Aug 21, 2006
Posts: 10
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

Joined: Aug 26, 2006
Posts: 4968
    
    1

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Throws and throw clause