Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Console Printing Order in Eclipse [duplicate]

I have a simple Arithmetic Program typed in Eclipse. When run the program in Eclipse the output appears in weird order each time I run. Sometimes The exception comes at last print statement comes first (which is the right way). Some times it comes vise-versa in a jumbled up order. Why it is happening & how to rectify it? Is there any setting to make it print in a right way each time I execute. The below screen shots show how it appears. Please help me rectify this problem.

Correct Order: Correct Order when run

Incorrect Order when we run couple times after Incorrect Order

 package com;

    public class Abc {

        /**
         * @param args
         */
        public static void main(String[] args) {

            System.out.println("begin main");
            // TODO Auto-generated method stub
            int a = 10;
            int b = 0;
            int c = 0;


            System.out.println("value of a BD is " + a);
            System.out.println("value of b BD is " + b);
            System.out.println("value of c BD is " + c);

            c = a/b; //Arthmetic Exception

            System.out.println("value of a AD is " + a);
            System.out.println("value of b AD is " + b);
            System.out.println("value of c AD is " + c);



        }

    }
like image 960
bali208 Avatar asked Oct 28 '13 17:10

bali208


1 Answers

Two different streams are being used here. You are using System.out. and the exception is printed with System.err. They are buffered, so one's output may print before the other's.

You can call System.out.flush(); before the exception line:

System.out.flush();
c = a/b;  //Arthmetic Exception
like image 143
rgettman Avatar answered Oct 21 '22 03:10

rgettman