• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception

 
sreelakshmi sarma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

2. Change the some2() method declaration to throw IOException inaddtion to FileNotFoundException.

3. Change the some2() method declaration to throw IOException only, since it is parent class of FileNotFoundException.

Hope this helps.
 
sreelakshmi sarma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Manju & Maha.
 
sreelakshmi sarma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. You can use both in a method/constructor.
regds
maha anna
 
sreelakshmi sarma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anna.
 
Naruki
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic