Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / November 2003

Tip: Looking for answers? Try searching our database.

Can't get try/catch block compiled successfully

Thread view: 
lonelyplanet999 - 29 Nov 2003 20:16 GMT
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");
   }
//============================
 }

}
Jose Rubio - 29 Nov 2003 22:49 GMT
EOFException and FileNofFoundException are not in the same hierarchy
structure. Well, they are, but they are sisters (or brothers??) So it
doens't matter which one you throw, the other one will be unreachable.

Hope it helps.

Jose

> Hi,
>
[quoted text clipped - 52 lines]
>
> }
lonelyplanet999 - 30 Nov 2003 07:25 GMT
> EOFException and FileNofFoundException are not in the same hierarchy
> structure. Well, they are, but they are sisters (or brothers??) So it
> doens't matter which one you throw, the other one will be unreachable.

So you mean there's no way to use one try/catch block to catch either
of these exceptions using one method ? :)

> Hope it helps.
>
[quoted text clipped - 56 lines]
>  
> > }
Thomas Schodt - 30 Nov 2003 08:10 GMT
>> EOFException and FileNofFoundException are not in the same hierarchy
>> structure. Well, they are, but they are sisters (or brothers??) So it
[quoted text clipped - 3 lines]
> So you mean there's no way to use one try/catch block to catch either
> of these exceptions using one method ? :)

There is.
Something like
if () throw new FileNotFoundException();
else throw new EOFException();
lonelyplanet999 - 30 Nov 2003 17:48 GMT
> >> EOFException and FileNofFoundException are not in the same hierarchy
> >> structure. Well, they are, but they are sisters (or brothers??) So it
[quoted text clipped - 8 lines]
> if () throw new FileNotFoundException();
> else throw new EOFException();

Tks;)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.