• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Confused over IOException  RSS feed

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Im getting a bit confused over IOException. According to the API, IOException is a checked exception.

I thought checked exceptions which occur are picked up at compile-time and runtime exceptions are highlighted at runtime.

So, if I compile the following code with a no-existent file called 'test.txt' the code will compile ok but will give me a checked IOException at runtime.


Please can anyone explain?


===================================================================

import java.io.*;
import java.util.Scanner;

public class myScanTest{
public static void main(String[] args) throws IOException {
Scanner s = null;
try {

s = new Scanner(new BufferedReader(new FileReader("test.txt")));

while (s.hasNext()) {
System.out.println(s.next());

}
}
finally {
if (s != null) {
s.close();
}
}
}
}



===================================================================


Thanks
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Santiago

FileReader throws a checked exception, the statement is inside try{} so it's OK. But since you provided a fictitious file, a exception is thrown, but based in your code, there is no catch statement for the thrown exception, so JVM will throw it at run time. The finally statement will always run. Add this..
catch(IOException ex){
System.out.println(ex.getMessage());
}

I hope this helps
[ August 09, 2008: Message edited by: Quirino Gervacio ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Santiago,

For checked exception, compiler complains you to handle or declare that exception. Code won't get compiled until you do that. Whereas for unchecked exception, compiler does not bother you to handle or declare that exception.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep..
The compiler does not flag a compiler error.
Checked exception only means, "the compiler ensures that the code giving out a checked exception is put inside a try block or declared as thrown"
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!