Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!