Can't get try/catch block compiled successfully
From: lonelyplanet999 (lonelyplanet999_at_my-deja.com)
Date: 11/29/03
- Next message: Connell Gauld: "Ahhhh...."
- Previous message: pete kirkham: "Re: map two data structure"
- Next in thread: Jose Rubio: "Re: Can't get try/catch block compiled successfully"
- Reply: Jose Rubio: "Re: Can't get try/catch block compiled successfully"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 29 Nov 2003 12:16:28 -0800
Hi,
I'm studying 310-035 exam with "Java 2 - Sun Certified Programmer &
Developer for Java 2 (310-035)" by Kathy Sierra, Bert Bates as
reference book.
I met one question about exception that confuses me.
The question asked what would happened if below code segment (enclosed
by /====) were placed into a class. The correct answer was 'The code
will not compile because of System.out.print statement just before the
first catch block. It also said that if this statement were removed,
the code would compile and code output: Start Hello world File Not
Found.
However, as I removed this statement I found compilation failed with
below error:
Ch4q11a.java:16: exception java.io.EOFException is never thrown in
body of corresponding try statement
catch (EOFException e) {
^
1 error
Actually, whether I placed throw new FileNotFoundException or throw
new EOFException or not placing any throw exception statement in the
try block makes no difference: the compiler either complained the
throw exception statment unreachable or the exception statement is
never thrown in the body of the corresponding try statement.
Can I trust the book's answer description ? If yes, what code still
missing ?
Tks 3:o)
import java.io.*;
class Ch4q11a {
public static void main (String [] args) {
//============================
System.out.print("Start ");
try {
System.out.print("Hello world");
throw new FileNotFoundException();
}
System.out.print(" Catch Here ");
catch (EOFException e) {
System.out.print("End of file exception");
}
catch (FileNotFoundException e) {
System.out.print("File not found");
}
//============================
}
}
- Next message: Connell Gauld: "Ahhhh...."
- Previous message: pete kirkham: "Re: map two data structure"
- Next in thread: Jose Rubio: "Re: Can't get try/catch block compiled successfully"
- Reply: Jose Rubio: "Re: Can't get try/catch block compiled successfully"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|