class ThreeException extends Exception {

/**
     * 
     */
    private static final long serialVersionUID = 1L;
static int count = 0;



    public static void main(String[] args) {
        while(true){
            try{
                if(count++ == 0)
                    throw new ThreeException();
                System.out.println("No Exception");     
            } catch(ThreeException e){
                System.err.println("Three Exception");
            } finally {
                System.err.println("In finally clause");
                if(count == 2) break;
            }
        }
    }

}

It prints:
Three Exception
In finally clause
In finally clause
No Exception

My question is why it doesnt print like this:
count = 0 -->
Three Exception
In finally clause
count = 1 -->
No Exception
In finally clause
count = 2 -->
No Exception
In finally clause

when count = 1 and 2 if statement doesn't excecute so it should print No Exception and then print the Finally
can someone explain it to me :)

You are "printing" to two separate PrintStream objects (out and err). In your case, they both output to the same place (i.e. the console, or your IDE's output); however, they both use internal buffers that are dependant on the object. This is the same way files tend to work: data is not guaranteed to be writtern out when you call a print() method, it is stored in the buffer. At some point, data will be written out to the console, but it is handled automatically.

To test this out, try only using out (or err). This should give you the results you expect, as each print statement will be stored sequentially in the same buffer. In order to use out and err, and have them print in the correct order, you'll have to use the flush() method. This forces data in the buffer to be written out, like so:

class ThreeException extends Exception {
    private static final long serialVersionUID = 1L;
    static int count = 0;
    public static void main(String[] args) {
        while(true){
            try{
                if(count++ == 0)
                    throw new ThreeException();
                System.out.println("No Exception");     
                System.out.flush();
            } catch(ThreeException e){
                System.err.println("Three Exception");
                System.err.flush();
            } finally {
                System.err.println("In finally clause");
                System.err.flush();
                if(count == 2) break;
            }
        }
    }
}

This is done this way for performance reasons, and you generally want to avoid calling flush() (especially for files) until you are done with the particular stream, or (as in your circumstance) you want to guarantee data has been completely written to the underlying data structure.

Because you use count++ in the if condition, you would confuse yourself. Think about what happen right after the condition? The count value is increased right after the comparison before throwing the exception statement.

if (count++ == 0 )
  Throw new ThreeException();

// is similar to...

if (count==0) {
  count++;
  Throw new ThreeException();
}
else {
  count++;
}

Though, I thought the order should be...
Three Exception
In finally clause
No Exception
In finally clause

The step should goes as follows:

/*
Let starts from entering the try at line 13
count = 0
Line 14-16
  count == 0 is true
  count = 1  <-- change the value right after the if statement
  Throw ThreeException()
Line 17-18
  catch the exception and display "Three Exception"
Line 19-20
  do the finally and display "In finally clause"
Line 14-16
  count == 0 is false (count is 1)
  count = 2  <-- change the value right after the if statement
  display "No Exception"
Line 19-20
  do the finally and display "In finally clause"
  break because count == 2 is true
*/

I might have missed something...

Edit: Ah, nmaillet pointed out that .out and .err. I knew that I missed something... If change to the same print stream, I would expect the output to be what I think... Or I am still missing something???

Edited 4 Years Ago by Taywin

Thank you guys for you quick repies.
This code was an answer from a test i had the previous semester and was asking what this code will print.

According to Eclipse it will print :
Three Exception
In finally clause
In finally clause
No Exception

with Taywin's logic should print:
Three Exception
In finally clause
No Exception
In finally clause

After reading it is more logical to print Taywin's but why Eclipse is printing it this way...
Explain the thought process please

Oh i am sorry my phone must have bugged or something i couldn't see your post.
Thank you it really clears it out !!! You are the man ;)

This question has already been answered. Start a new discussion instead.