This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
import java.io.*; class testexception { public void some() throws IOException { System.out.println("some"); } public void some2() throws FileNotFoundException { some(); System.out.println("some2"); }
public void some3() { try { some2(); } catch(IOException e) { System.out.println("i got you"); } } public static void main(String args[]) { testexception te1 = new testexception(); te1.some3(); } } It's giving me a compile time error that IoException must be declared or it must be caught. But i am catching IOException in the some3. Could somebody please explain this to me. Thanks.
maha anna
Ranch Hand
Joined: Jan 31, 2000
Posts: 1467
posted
0
Sree,Use the [ CODE ] [ /CODE ] tags when you post your code. The compile error is because of this code. <pre> public void some() throws IOException{ System.out.println("some"); } public void some2() throws FileNotFoundException{ // either this shd be changed to IOException or the foll. some(); method invocation MUST be enclosed with try..catch(IOException) some(); System.out.println("some2"); } </pre> regds maha anna
[This message has been edited by maha anna (edited March 09, 2000).]
Manju Swamy
Greenhorn
Joined: Mar 03, 2000
Posts: 14
posted
0
Exception hierarchy <PRE> +--java.lang.Exception | +--java.io.IOException | +--java.io.FileNotFoundException </PRE> Here are some of the possible choices: 1. You can enclose the call to some() method in try ... catch block.
Mind is like a parachute. It works only when it is open.
sreelakshmi sarma
Ranch Hand
Joined: Mar 02, 2000
Posts: 130
posted
0
Thank you very much Manju & Maha.
sreelakshmi sarma
Ranch Hand
Joined: Mar 02, 2000
Posts: 130
posted
0
One small question though. I think i read somewhere that "throws" and "try & catch" cannot be put together. Can someone clarify this to me.
maha anna
Ranch Hand
Joined: Jan 31, 2000
Posts: 1467
posted
0
throws is different from throw keyword. 1. 'throw' may/may not have try..catch statements <pre> ex. void m1() throws IOException{ throw new IOException(); } ex. void m2() { throw new UnknownError(); } ex. void m3() { try { throw new IOException(); }catch( IOException e) {} } 2.'throws' comes in method and constructor declarations ex. void m1() throws IOException { throw new IOException(); } ex. test() throws Exception { //constructor throwing exception } </pre> regds maha anna
[This message has been edited by maha anna (edited March 09, 2000).]
sreelakshmi sarma
Ranch Hand
Joined: Mar 02, 2000
Posts: 130
posted
0
Maha Anna i don't understand your point.I know that "throw" and "throws" are two different keywords. I am not confused about that. My question is can "try & catch" and "throws" be put together.Please take a look at the following code. CODE: import java.io.*; class TestException { public void some() throws IOException { System.out.println("some"); } public void some2() throws FileNotFoundException { try { some(); } catch (IOException e) { System.out.println("i got you some()"); } System.out.println("some2"); } } I hope i making sense now. Thanks.
maha anna
Ranch Hand
Joined: Jan 31, 2000
Posts: 1467
posted
0
Yes. You can use both in a method/constructor. regds maha anna
sreelakshmi sarma
Ranch Hand
Joined: Mar 02, 2000
Posts: 130
posted
0
Thanks Anna.
Naruki
Greenhorn
Joined: Oct 29, 2001
Posts: 3
posted
0
I thought that you couldn't use "throws" if the try{...} catch{...} block caught the same exception, unless you rethrew it. Apparently I was wrong. Here's some very simple code to illustrate:
[This message has been edited by Naruki (edited November 01, 2001).]