Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at

Monday, July 7, 2014

OCJP practice question #25

What will be the output  of following java program? Assume that File1.txt exists.

class TestIO
        public static void main(String[] args)
                        FileReader fis=new FileReader("File1.txt");
                        FileWriter fos=new FileWriter("File1Output.txt");
                        int data;
                catch (Exception e)
                catch (IOException ioe)

  1. Output file will have same content as in input File1.txt.
  2. An exception will be raised during run time.
  3. Compilation fails.
  4. Nothing will be printed in output file.


Option C is correct, a compile time error will be thrown by the compiler because of the catch block ordering.


Since Exception is a super class of all exceptions, any exception raised during the flow of program will go to first catch block always, never in second block.

You can write multiple catch blocks in java but child exceptions must come first than parent exceptions. (Vice versa makes the compilation process fail.)

Also, note that sibling exceptions order in catch block does not matter. 

No comments: