File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes throw and throws Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "throw and throws" Watch "throw and throws" New topic
Author

throw and throws

Ravissant Markenday
Ranch Hand

Joined: Nov 12, 2006
Posts: 47
hi everyone!

Could anyone please tell me the diffrence between the "throw" and "throws" clauses in exception handling?

thanks in advance,
Ravissant Markenday
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

throw - used when you want to throw exception explicitly. you can throw any exception which subclass of Throwable interface.

Example:

throws - used when you think that your method may throw exception, so caller should take care of it.

Example:
This method is from FileInputStream class. It says, if you use read() method then you need to catch IOException.


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Ravissant Markenday
Ranch Hand

Joined: Nov 12, 2006
Posts: 47
Thanks for the reply Prabhu, so can both "throw" and "throws" be used for checked and unchecked exceptions???

Thanks again,

Warm Regards,
Ravissant Markenday
Ravissant Markenday
Ranch Hand

Joined: Nov 12, 2006
Posts: 47
yes and one more question here, does jvm handle both checked and unchecked exceptions??

Thanks,

Regards,
Ravissant Markenday
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

can both "throw" and "throws" be used for checked and unchecked exceptions???


Yes, you can.However, if throw unchecked exceptions using throw or throws, it is not mandatory for caller to handle it. means caller can call your method without try{}catch(){}.
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

does jvm handle both checked and unchecked exceptions??


How JVM knows, how to handle the exception which occurred in your code?

JVM is the one who throws those exception, saying "Buddy, something wrong with your code, check it!".

hth,
Ravissant Markenday
Ranch Hand

Joined: Nov 12, 2006
Posts: 47
Thanks Prabhu !!!
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

One small correction on the first reply to this question - Throwable is not an interface, but a perfectly instantiable class.
 
 
subject: throw and throws
 
Similar Threads
Difference
Throws and throw
throws and throw
diff b/w Throw and Throws
throw throws