Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Some questions on the following code...

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys!

Not sure if this should be in another section but I'm studying for the SCJP so here I am...

For the following code, I have the following questions:
1.) Why does this compile dispite there not being and FileNotFoundException,
2.) I tried to put a "finally" statement in this code but could not get it to compile, why is this?


I appreciate it!


[BPSouther: Added code tags]
[ March 20, 2008: Message edited by: Ben Souther ]
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"mrKitKat.isMember()",
Please check your private messages regarding an important administrative matter.
-Ben
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It compiles without an explicit catch for FileNotFoundException because you are catching IOException which is the super class to FileNotFoundException.
http://java.sun.com/j2se/1.5.0/docs/api/java/io/FileNotFoundException.html

In other words, you're catching anything that is an IOException and because FileNotFoundException is an IOException, the catch block that you have covers it.

Without seeing your finally clause and the compiler error, it would be hard to say what the problem was when you tried to add it.
 
Claude Cundiff
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the modified code with the finally clause.



[BPSouther: Added code tags]
[ March 20, 2008: Message edited by: Ben Souther ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm guessing that the compiler is complaining that inputStream is not defined.

You've defined it in your try block (where the finally block can not see it).

To make a variable available to both, you would need to define it outside of the try/catch/finally block


[ March 20, 2008: Message edited by: Ben Souther ]
 
Claude Cundiff
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought so too. I tried to use the following modification but got the following from the compiler:

Java:28 unreported exception java.io.IOException; must be caught or declared to be thrown inputStream.close();
^

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;

public class Ready {

FileReader inputFileReader = null;
BufferedReader inputStream = null;

//Define Constructor
Ready(){
try {

String inputFileName = "C:/Core/data.txt";
inputFileReader = new FileReader(inputFileName);
inputStream = new BufferedReader(inputFileReader);
String inLine = null;
while((inLine = inputStream.readLine()) != null){
System.out.println(inLine);
}

inputStream.close();

}catch(IOException e) {
System.out.println("Boo!");
e.printStackTrace();
}
}

public static void main(String[] args){
new Ready();
}
}
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your program will compile successfully because you are catching IO exception. Compiler never validates existence of file and its location. It happens at runtime. So, only at runtime you will get FileNotFoundException exception not at compile time.

In finally block, you have used inputStream variable which is declared in try block. Compiler can not make sure whether this try block will run or not. If try block wont run, inputStream.close(); in finally block will get failed. Since, here is a doubt in declaration of inputStream , you will get a compile time error if you use this statement in finally block.

Use a variable outside of any block where it is defined. You will get a compile time error if there is even a minuscule chance of not executing this block.
[ March 20, 2008: Message edited by: agilemanoj kumar ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a close method throws an exception, you may have to provide a nested try/catch block.



You'll see this a lot in JDBC examples where closing the database connection throws exceptions.
[ March 20, 2008: Message edited by: Ben Souther ]
 
Claude Cundiff
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ben,

Thanks! That works.

I've seen code that uses if...else statements in the finally clause that illustrates this. So, I guess that makes sense.

I really appreciate it!
 
agilemanoj kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Claude,
Your last posted code will compile fine...

Hey Ben, could you please post updated code over here... I am not able to get your last posted a part of code[finally block code]...
 
Claude Cundiff
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks agilemanoj! Here is the final product. It just reads a text file and prints it out on the console.

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;

public class Ready {
//Define Constructor
Ready(){
FileReader inputFileReader = null;
BufferedReader inputStream = null;
try {

String inputFileName = "C:/Core/data.txt";
inputFileReader = new FileReader(inputFileName);
inputStream = new BufferedReader(inputFileReader);
String inLine = null;
while((inLine = inputStream.readLine()) != null){
System.out.println(inLine);
}

//inputStream.close();

}catch(IOException e) {
System.out.println("Boo!");
e.printStackTrace();
} finally {
try{
inputStream.close();
} catch(Exception ignored){
inputStream = null;
}
}
}

public static void main(String[] args){
new Ready();
}
}
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Claude,
Javaranch tip:

If you surround your posted code with UBB CODE tags] your indenting will be preserved making your code much easier to read.
Code that's easy to read tends to get faster and better answers.
 
Claude Cundiff
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure thing. Yeah, it's kind of mess as it stands now
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic