1

I am getting different output in each run of my program. When executed first time, it givesenter image description here

When executed again, it enter image description here Please tell me why it is occurring.

public class TwoExcepProg {
    public static void main(String[] args) {
        try
        {
            int a = 50/0;
        }
        finally
        {
            System.out.println("finally block is called");
        }
    }
}
dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
Vikas
  • 19
  • 2
  • Took me a second to get there. Problem is that the two messages go to **two** different streams. – GhostCat May 20 '17 at 20:08
  • @GhostCat just to stay clear of any danger in the future, I didn't downvote your answer :) – dumbPotato21 May 20 '17 at 20:11
  • @ChandlerBing Thanks for the information. But dont you worry. I absolutely hate **revenge** downvoters. I maybe do sometimes things that are not 100% according to the strict rules here; but I never ever downvote content because I had "issues" with another users. Downvotes go out for content I disapprove with; for nothing else. – GhostCat May 20 '17 at 20:13
  • And finally, to address your comment: execution order is try block, catch block, finally block. Exactly in that order. As it is written down in any book or tutorial for this topic. – GhostCat May 20 '17 at 20:16

1 Answers1

3

You let propagate an exception that makes your program crashed.

In these conditions, you should not expect to have a gracious and rigorous order appearing of the output of the exception that has stopped the program (output written by the System.err.println() method) and the output written in the finally statement (output written by the System.out.println() method) since these are two distinct streams not necessarily flushed at the same time.

You should handle the exception to prevent this behavior :

public static void main(String[] args) {
    try {
        int a = 50 / 0;
    } 
    catch (Exception e) {
        // exception handling
    }
    finally {
        System.out.println("finally block is called");
    }
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • 2
    DV, because the explanation is wrong. The fact that OP uses an exception doesn't matter here. The issue is _solely_ based on the fact that OP writes to two different streams which are independently flushed. – Tom May 20 '17 at 20:13
  • You can test this with a simple `for (int i = 0; i < 5; i++) { System.out.println(i); System.err.println(i); }`. – Tom May 20 '17 at 20:16
  • 1
    Two down votes, waouh :) @Tom thank your for your fair :) It is not really right : the OP doesn't write to two different streams. He writes to a stream (System.out). The System.err writing is caused because he lets the exception crash the program. – davidxxx May 20 '17 at 20:18
  • Never mind. Good days, bad days. I only had a single upvote today, so, whatever. You know what, you got your upvote back; trusting you to improve your question later on. – GhostCat May 20 '17 at 20:21
  • 1
    @Tom The explanation was really not wrong. I had not just detailed these points. I updated it. – davidxxx May 20 '17 at 20:25
  • 1
    Oh yes, that's correct, he indirectly writes to "serr" due to the exception. Thanks for updating the answer. – Tom May 20 '17 at 20:25
  • @GhostCat Thank you for your kindness. You are the best :) Some days are sometimes not good indeed. – davidxxx May 20 '17 at 20:26
  • @Tom You are welcome. Anyway, you was right to alert me about these details that are important to give a better explanation. – davidxxx May 20 '17 at 20:30