Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Error

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
void priya() {
System.out.println("Start");
try{
System.out.println("Hello world ");
throw new FileNotFoundException();

}catch(EOFException e){//---------------------error
System.out.println("End of file");
}catch(FileNotFoundException ae){
System.out.println("File not found");
} finally{
System.out.println("Finally");
}
}

I got the error EOFException is never thrown in corresponding try statement.But the book says it will display
Start
Hello World
File not found
Finally.
 
Michael Valentino
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this a question? What book are you referring to? Is this the complete code sample? If it is, then you're correct. The EOFException will be seen by the compiler as unreachable code, and won't compile.
[ June 07, 2006: Message edited by: Michael Valentino ]
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println("Start");
try{
System.out.println("Hello world ");
throw new FileNotFoundException();

}catch(EOFException e){//---------------------error
System.out.println("End of file");
}catch(FileNotFoundException ae){
System.out.println("File not found");
} finally{
System.out.println("Finally");
}
}

And given EOFException and FileNotFoundException are subclasses of IOException and further assuming this block of code is placed into a class.This is the question given.

I am referring K&B book.
 
S Thiyanesh
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EOFException is a checked exception.
To catch any of the checked exceptions, there must atlest be one statement
in the try block that may cause the same or its subclass exception.

so if you have

there will be a compiler error.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic