Author Topic:   Exception
sree
ranch hand
posted March 09, 2000 11:07 AM             
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
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.

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");
}

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

+--java.lang.Exception
|
+--java.io.IOException
|
+--java.io.FileNotFoundException

Here are some of the possible choices:

1. You can enclose the call to some() method in try ... catch block.

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");
}

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();
}
}


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

code:

import java.io.*;

class TestException {
public void some() throws IOException {
System.out.println("some");
}

public void some2() throws FileNotFoundException, IOException {
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();
}
}


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

code:

import java.io.*;

class TestException {
public void some() throws IOException {
System.out.println("some");
}

public void some2() throws IOException {
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();
}
}


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

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
}

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.*;
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
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.

|