This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Method with throws Exception in declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method with throws Exception in declaration" Watch "Method with throws Exception in declaration" New topic
Author

Method with throws Exception in declaration

Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
when you have like



does this require you to do a try/catch/finally everytime
you call this method?

Thanks,

Justin


You down with OOP? Yeah you know me!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11444
    
  16

no.

well, not exactly.

you have to either catch and handle the exception, or re-throw it. i could do this:




so while i am not explicitly using a try/catch/finally, whatever calls my cook() method has to... or has to re-throw it.

SOMETHING SOMEWHERE will have to catch the exception, but it may not be anything you ever wrote.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Are this and this interview/book questions?

Did you give it a try?
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
when i get home i'll actually start testing some of this stuff out, but from the example I didn't know
whether adding 'throws IOException' or any other exception required the caller of that function
to do a try/catch or not.

But now I see that you can also call that function inside another that throws the general exception
'throws Exception' or the same, or a sub-exception of the original.

Thanks,

Justin

P.S
I'm just running through the SCJP study guide and I'm asking questions of things I'm not 100% on.
I really want to get a grasp of this and get my Certs. Now that I have graduated and have more
free time on my hands I can actually study for this more.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Never mind.
Make sure you test them actually because we may lie but compiler won't.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method with throws Exception in declaration