The program below was taken from the official SunJava tutorial. It demonstrates how the ByteStream classes like FileInputStream and FileOutputStream work. The program reads characters from one file and puts them into another file. I made the input file - xanadu.txt - unavailable (i.e., did not create the file). This results in an exception at line 10 and control immediately jumps to the finally block.
My problem is with the two if statements in the finally block. The second if statement - if(out!=null) - evaluates to true and "out.close()" is executed even though "out" is equal to null. The problem, however, corrects itself when I put the if(out!=null) statement above the if(in!=null) statement. Why is the result changing based on the placement of the if statement?
Its strange. I tried using System.out.print inside " if(out!=null) ". It (System.out.print) was not executed, but the out.close() line was. This happened even though both those statements were within a block:
System.out.print("hello");//was skipped over