File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Is That true About Throws Exception ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is That true About Throws Exception ??" Watch "Is That true About Throws Exception ??" New topic

Is That true About Throws Exception ??

Matt Swaggi
Ranch Hand

Joined: May 29, 2008
Posts: 61
Whenever the program does not want to handle exception using try block it can use the throws clause,can you explain more about throws !
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2904

throws clause comes with method,

when any method performing any operation which throws any "Exception" (Checked or unchecked), either you can handle inside that same method body , using try {...} catch block OR simply "throws" that "Exception" to caller method !!


public void methodWhithThrows() throws Exception
// some IO operation which throw IOException
// you need not to handle this exception here
// because you already mentioned it that , this
// method "throws" any exception to caller method

Hope this clarifies the things !

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

The Java Tutorial's section on exceptions might help you understand this. It explains in a bit more detail then we could here.

In a nutshell, re-throwing an exception simply means that instead of handling it, the code is going to let who ever called it handle it. Eventually though, it needs to be handled by a catch block. So say MethodOne calls MethodTwo, and MethodTwo calls MethodThree, and MethodThree can throw a BigMistakeException. MethodTwo can either handle that exception via a catch block, or rethrow it by declaring that it throws a BigMistakeException. Now MethodOne must either handle the exception, or rethrow it.

This is done so the Exception can be handled in the best place possible; the place where the best decision about what to do can be made.

Be sure to take a look at the tutorial; there's a lot more to it than my simple summary.
Matt Swaggi
Ranch Hand

Joined: May 29, 2008
Posts: 61
Thank you Sir !

[edit]Get rid of CAPITALS. CR[/edit]
[ July 18, 2008: Message edited by: Campbell Ritchie ]
I agree. Here's the link:
subject: Is That true About Throws Exception ??
It's not a secret anymore!