• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what does throws does ?

 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
atleast reply my last query ??
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic