This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception" Watch "Exception" New topic
Author

Exception

sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
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
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
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.


Mind is like a parachute. It works only when it is open.
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Thank you very much Manju & Maha.
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
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
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
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
Yes. You can use both in a method/constructor.
regds
maha anna
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Thanks Anna.
Naruki
Greenhorn

Joined: Oct 29, 2001
Posts: 3
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).]
 
Don't get me started about those stupid light bulbs.
 
subject: Exception
 
Similar Threads
Exception and Instance Initializer
Unreported exception Problem
Concept on coding for Exception Handling
can anyone explain me this?