Author | Topic: Exception |
sree ranch hand |
posted March 09, 2000 11:07 AM
import java.io.*; class testexception { public void some() throws IOException public void some2() throws FileNotFoundException
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 bartender |
posted March 09, 2000 11:30 AM
Sree,Use the [ CODE ] [ /CODE ] tags when you post your code. The compile error is because of this code.
regds maha anna [This message has been edited by maha anna (edited March 09, 2000).]
|
Manju Swamy greenhorn |
posted March 09, 2000 12:20 PM
Exception hierarchy
Here are some of the possible choices: 1. You can enclose the call to some() method in try ... catch block. code: 2. Change the some2() method declaration to throw IOException inaddtion to FileNotFoundException. code: 3. Change the some2() method declaration to throw IOException only, since it is parent class of FileNotFoundException. code: Hope this helps.
|
sree ranch hand |
posted March 09, 2000 12:36 PM
Thank you very much Manju & Maha.
|
sree ranch hand |
posted March 09, 2000 12:41 PM
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 bartender |
posted March 09, 2000 01:38 PM
throws is different from throw keyword. 1. 'throw' may/may not have try..catch statements
regds maha anna [This message has been edited by maha anna (edited March 09, 2000).]
|
sree ranch hand |
posted March 09, 2000 01:47 PM
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.*; I hope i making sense now. Thanks.
|
maha anna bartender |
posted March 09, 2000 02:11 PM
Yes. You can use both in a method/constructor. regds maha anna
|
sree ranch hand |
posted March 09, 2000 02:42 PM
Thanks Anna.
|
| | |