File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Difference between throw and throws Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between throw and throws" Watch "Difference between throw and throws" New topic
Author

Difference between throw and throws

Vasiq Molvizadah
Ranch Hand

Joined: Dec 24, 2009
Posts: 66
Hi guys....i'm new to Java and i've been reading Head First Java book, there is one part which i didn't get it is...
What's the difference between "throw" and "throws" clause ...and why do we use these two ???
Thanks for reading it....


Human Knowledge Belongs to the World.
- Vasiq Mz
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

One defines what a method throws. One actually throws something. Did you search for Java documentation covering those keywords?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
The "throw" keyword causes an Exception object to be created. The throws keyword allows one to warn the compiler that a certain method might throw an Exception.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm not sure I'd say that "throw" *causes* an exception object to be created; you still need to create the exception, no?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Correct. That's what the "new" after the "throw" does. You can also throw existing exceptions, like "throw e;".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
You're right; that is more precise than what I wrote.
 
 
subject: Difference between throw and throws
 
Similar Threads
Difference
throw and throws
throws and throw
Throws and throw
difference between throw and throws