Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Netbeans and Eclipse compiler's output is different. My book's output is too...

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Netbeans and Eclipse compiler's output is different. My book's output is too...

    Hi,
    I was trying to execute the following codes, but the something that I don't undestand was happen. The program was compiled differently according to ouput picture of the program in my java book. Furthermore, then I tried to compile the program in eclipse and NetBeans. I saw that all of output are different each other. I think output of the book is sensible, but I couldn't see this output on the compilers. Can anybody explain this stutation? What is the problem?

    package finallyblock;
     
    public class FinallyBlock {
        public static void main(String[] args) {
            try{
                throwException();
            }
            catch(Exception exception){
                System.err.println("Exception handled in main");
            }
     
            doesNotThrowException();
        }
     
        public static void throwException() throws Exception{
            try{
                System.out.println("Method throwException");
                throw new Exception();
            }
            catch(Exception exception){
                System.err.println("Exception handled in method throwException");
            }
            finally{
                System.err.println("Finally executed in throwException");
            }
        }
     
        public static void doesNotThrowException(){
            try{
                System.out.println("Method doesNotThrowException");
            }
            catch(Exception exception){
                System.err.println(exception);
            }
            finally{
                System.err.println("Finally executed in doesNotThrowException");
            }
     
            System.out.println("End of method doesNotThrowException");
        }
    }

    The output In My Book:
    Method throwException
    Exception handled in method throwException
    Finally executed in throwException
    Exception handled in main
    Method doesNotThrowException
    Finally executed in doesNotThrowException
    End of method doesNotThrowException


    The output On NetBeans:
    Method throwException
    Method doesNotThrowException
    Exception handled in method throwException
    End of method doesNotThrowException
    Finally executed in throwException
    Finally executed in doesNotThrowException



    The output On Eclipse:
    Method throwException
    Exception handled in method throwException
    Finally executed in throwException
    Method doesNotThrowException
    End of method doesNotThrowException
    Finally executed in doesNotThrowException


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Netbeans and Eclipse compiler's output is different. My book's output is too...

    As far as I understand the code the eclipse output is correct.
    The output from your book is not correct because the exception that was thrown in the method throwException() will never reach the main method since it is already caught in the method itself.
    So the text "Exception handled in main" should never be printed.

    Why the output from netbeans is so different I can not explain. This would only make sense if you used multithreading which you obviously dont. At least judging from the code you've posted.

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Netbeans and Eclipse compiler's output is different. My book's output is too...

    Firstly I cannot believe that my book is not correct, but you said that "'Exception handled in main' should never be printed." and I think it is sensible. Secondly I learnt the reason of different ouputs on the compilers from another forum. According to a user from coderanch forum, the reason is that: "the reason is because you're writing some messages to System.out, and some messages to System.err. These streams are being flushed at unpredictable times, so their output may appear in random order in respect to each other."

    Netbeans and Eclipse compiler's output is different. My book's output is too... (Java in General forum at JavaRanch)

    Edit:
    Sorry, I noticed that I wrote the code incomplete. I must add a line to the program above:

    throw exception; // inside catch block in throwException() function
    Last edited by hefese; September 6th, 2014 at 04:45 PM.

  4. #4
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Netbeans and Eclipse compiler's output is different. My book's output is too...

    Quote Originally Posted by hefese View Post
    Firstly I cannot believe that my book is not correct, but you said that "'Exception handled in main' should never be printed." and I think it is sensible. Secondly I learnt the reason of different ouputs on the compilers from another forum. According to a user from coderanch forum, the reason is that: "the reason is because you're writing some messages to System.out, and some messages to System.err. These streams are being flushed at unpredictable times, so their output may appear in random order in respect to each other."
    This is likely, and is an example of undefined behaviour -- that is to say, it isn't accurate to say that any of the runtime environments you mention are doing anything "correctly".

    But, you can prove it. System.out is a PrintStream, and PrintStream is Flushable. Invoking .flush() at every turn will essentially turn buffering off.

    BTW, this aspect of buffering is why logging is not a dependable timing mechanism.

  5. #5
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Netbeans and Eclipse compiler's output is different. My book's output is too...

    Quote Originally Posted by hefese View Post
    Edit:
    Sorry, I noticed that I wrote the code incomplete. I must add a line to the program above:

    throw exception; // inside catch block in throwException() function
    If you add this line to your code then what your book says would be correct. Because now your are re-throwing the exception and it will reach main.

    By the way, books can have lots of errors, there are so many hands which a book has to go through until it is printed, it can easily happen that somebody messed up at some point. We are all just human.

  6. #6
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Netbeans and Eclipse compiler's output is different. My book's output is too...

    Okay, thank you guys for your replies.

Similar Threads

  1. [SOLVED] Java runtime get result output from prompt problem with a larger output in type
    By kingwang98 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 14th, 2014, 08:52 AM
  2. Eclipse Compiler Level Tuning
    By Ada Lovelace in forum Java IDEs
    Replies: 2
    Last Post: May 28th, 2014, 03:35 PM
  3. Why create eclipse this output ?
    By manzara in forum Java Theory & Questions
    Replies: 9
    Last Post: April 3rd, 2013, 07:16 AM
  4. how could I output to a text area the output of a method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 12th, 2012, 07:49 PM
  5. setting the compiler for eclipse mac os x
    By etidd in forum Java IDEs
    Replies: 2
    Last Post: January 29th, 2010, 10:18 AM

Tags for this Thread