This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Am I Insane -- You Tell me!!!!  RSS feed

 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
Hey,
Guys, I am a bit confused about these two codes. The first one does not compile, giving me the following compile time errors:
************ CODE ONE ****************

import java.io.*;
public class iokam6
{
public iokam6(String s1, String s2)
{
FileInputStream fin;
FileOutputStream fout;

try
{
fin=new FileInputStream(s1);
fout=new FileOutputStream(s2);
}
catch(Exception ex1)
{
System.out.println("Exception ex1 : "+ex1);
}

try
{
int i;
i=fin.read();
while(i!=-1)
{
fout.write(i);
i=fin.read();
}
}
catch(Exception ex2)
{
System.out.println("Exception ex2 : "+ex2);
}

try
{
fin.close();
fout.close();
}
catch(Exception ex3)
{
System.out.println("Exception ex3 : "+ex3);
}
}

public static void main(String args[])
{
new iokam6(args[0], args[1]);
}
}

*************************************************************************

The Errors Generated :

Variable fin may not have been initialized.i=fin.read();
Variable fout may not have been initialized.fout.write(i);

Variable fin may not have been initialized.fin.close();
Variable fout may not have been initialized.fout.close();
***********************************************************************

But the Following code compiles and runs.
******************** CODE TWO **********************
import java.io.*;
public class iokam6
{
public iokam6(String s1, String s2)
{
try
{
FileInputStream fin;
FileOutputStream fout;
fin=new FileInputStream(s1);
fout=new FileOutputStream(s2);

int i;
i=fin.read();
while(i!=-1)
{
fout.write(i);
i=fin.read();
}

fin.close();
fout.close();
}
catch(Exception ex2)
{
System.out.println("Exception ex2 : "+ex2);
}


}

public static void main(String args[])
{
new iokam6(args[0], args[1]);
}
}

*********************************************************

The only Difference between the two codes is using multiple try catch sets (as in the first code) instead of using a single try catch block (as in the later code).
Can anyone explain what is actually happening here???

Bye,
Tualha Khan
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tualha,
In Code 1, by placing the creation statements within a <code>try</code> block you are explicitly telling the compiler that the files may not be found. You do not specify any alternative handling if that turns out to be the case ... so, if they aren't found <code>fin</code> and <code>fout</code> will never be initialized and therefore can't be used by the remaining code. The compiler coughs up an error as it's not sure how to handle things.
In Code two, you take the initialization out of the <code>try</code> block; telling the compiler to assume the files will always be found and that <code>fin</code> and <code>fout</code> will always be available to the remaining code.
In this case, the compiler defers to the runtime. If it turns out the files can't be created the runtime will throw <code>FileNotFoundException</code> and the application will stop.
Hope that helps.
------------------

Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
Ranch Hand
Posts: 4702
9
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of what happens if an exception IS thrown. In code 2 the lines that produce the compile time error will be skipped(as execution jumps to the catch block), not so in code 1 therefore the error.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!