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 what does throws does ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what does throws does ?" Watch "what does throws does ?" New topic
Author

what does throws does ?

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
Hi,

i read from books but there is still dought regarding "throws" keyword..
i mean when we write like

retruntype methodname(arg1,arg2) throws IOException,FileNotFoundExcetion
{
........
........ something

}

what does that mean ...here above we don't write any try-catch statements ??
can you please expain in details what it is and how it works ???

thanx and regards,
amit


Thanks and Regards, Amit Taneja
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
The 'throws' keyword tells the compiler, and anybody calling that method, that that method can throw those exceptions.

So if you have:


if you call that method you must catch the exception:

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
so is that mean that we have to write the statemenet

if(problemHappens){
throw new BigBadException("Error Message");
}

in the method...
but what i have seen is that .. this re-through statement is not written or not necessary if u write excetion in calling method which you have also written but what i m asking is that ...is that above statement is neccessary ?

thanx for reply
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
You do not have to write the 'throw' statement. You method doesn't even have to throw any exception, you are just declaring that it can. Sometimes this is done to avoid catching an exception that is being thrown by a method that is being called.

Anandh Ramesh
Ranch Hand

Joined: Dec 15, 2004
Posts: 61
hi,

you may know that in java, there are a few exceptions that may or may not occur, like the ones in i/o for example. some of these must be handled, some of these need not be handled, both depending on the user. if the user decides that the exception must be handled, he may use a try...catch block or a throw. when the exception need not be handled, he uses a 'throws'. the exceptions that arise need not be caught in this case. for more information, you may see the 9th chapter of 'Thinking in Java' by Bruce Eckel.




cheers,<br />Anandh
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
but u peaple didn't tell me what i asked that

what is the use of "throws" with method name defination
what if don't write catch even at calling method end
?
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
read this:

exceptions tutorial

I don't think you're understanding the basic use of Exceptions in Java and you would be better served by a more in-depth tutorial rather than forum posts.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The "throws" clause in the method declaration tells us that the method can possibly throw an exception.

The calling code has a couple options for deailing with this. It can have a matching catch clause for the declared exception type or any supertype. Or it can have a matching throws clause in its declaration. Then any exception it doesn't catch can "bubble up" to the next calling method and so on until one of them catches it.

If the calling method doesn't do either of those things, the compiler will complain.

You will also see calling methods that catch one exception and rethrow it or throw some other exception. (The reasons to do this may be a bit deep for today, but holler if you're ready for more.) The method must have a "throws" clause for whatever it wants to throw.

I'll second the recommendation for Bruce Eckell's explanation on all this. His prose is pretty good, but I often wished his examples were a little better. Anyhow, "Thinking in Java" is available as a PDF for no cost on the web.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
ok now i bit got some idea from james...

what i understood is

we should write "throws" with method declaration if exception can arise in the method defination.. or we should but try-catch block

in former case ( i.e throws) we should catch the exception in calling method or else catch the exception to super calling method !!

is i m right ?

any way Thanx a ton..

one last thing.... most people refer here to read "Thinking in Java" ...is it really a good book ?

and what about the book from kethy-serra for scjp prepration.. shouldn't i read that book as it will serve double purpose ...one prepare me for scjp and clear my fundas.....
as i heard lot for kehty-serra book in this forum...
what should i do ?

which book to refer finally..


thanx
amit
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
atleast reply my last query ??
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yes, what you said looks pretty good. The calling method of course has all the same choices again ... catch or throw or whatever ... all the way up to main. If main lets an exception bubble out, the program is over.

You can read Thinking In Java here: http://www.mindview.net/Books/TIJ/ and maybe decide you'd rather have paper in hand.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what does throws does ?
 
Similar Threads
Y do u do this 2 ur posts
for ( ; ;)
servlet and jsp spec to be referred for scwcd
Variables names in Hex and Octal form
What does this mean?