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 8 of 8

Thread: Can Some one explain to me why the output will be like this...

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Can Some one explain to me why the output will be like this...

    public class Test {
     
    	static String s = "One ";
     
        public static void main(String[] args) {
        	new Test().method1();
        	System.out.println(s);
        }
     
        static void method1(){
        	try{
        		method2();
        	}
        	catch(Exception e){
        		s += "Two ";
        	}
        }
     
        static void method2() throws Exception{
        	s += "Three ";
        	method3();
        	s += "Four ";
        	method3();
        	s += "Five ";
        }
     
        static void method3() throws Exception{
        	s += "Six ";
        	throw new Exception();
        }
     
    }

    Why the output of this is not == > One Three Six Four Six Five
    Can someone explain what exception is occur and where it occur??


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,420
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Can Some one explain to me why the output will be like this...

    Please copy and paste here the full contents of the command prompt window that shows the programs output and errors etc.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Can Some one explain to me why the output will be like this...

    All the exceptions have been caught by the exception handle and the output for this coding is "One Three Six Two"... And I don't know how to use the command prompt to execute this coding. I'm using JCreator to execute. I wish someone can explain to me why the output is "One Three Six Two" instead of "One Three Six Four Six Five"... Anyway thanks for your reply. Much appreciated...

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,420
    Thanks
    49
    Thanked 2,215 Times in 2,188 Posts

    Default Re: Can Some one explain to me why the output will be like this...

    explain to me why the output is
    Add lots printlns to the code to show where the execution flow goes. Put one after every statement.
    The exception that is thrown changes the execution flow.

  5. The Following User Says Thank You to Norm For This Useful Post:

    lunchong (December 31st, 2011)

  6. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Can Some one explain to me why the output will be like this...

    I did tried to put a println after every statement but I wondering what kind of exception occur and changed the flow of execution. Any thing wrong with the way for this coding to call its function?

    And I tried to use command prompt to run it. There are no exception and this is the out put

    C:\Users\lun\Desktop>java Test
    One Three Six Two

    C:\Users\lun\Desktop>

  7. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Can Some one explain to me why the output will be like this...

    Can I ask why you're throwing an Exception inside method3.

    I removed the line

    throw new Exception();

    and it outputted

    One Three Six Four Six Five

    The way you told it to go, it seemed to be going into the catch block as you just told it to throw an Exception in method3 which was called by method2 which was called in the try block.
    Last edited by javapenguin; December 31st, 2011 at 10:56 PM.

  8. The Following User Says Thank You to javapenguin For This Useful Post:

    lunchong (December 31st, 2011)

  9. #7
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Can Some one explain to me why the output will be like this...

    Actually this is a question from my past year examination question and I'm doing my revision. The question is to trace the output. Since the output I traced doesn't same as the output after i execute it then i post here for someone to explain to me

  10. #8
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Can Some one explain to me why the output will be like this...

    Thanks to Norm and javapegiun. I think I'm understood why the output will be like this.

Similar Threads

  1. i need an explain please !
    By keep smiling in forum Java Theory & Questions
    Replies: 3
    Last Post: December 21st, 2011, 10:22 AM
  2. New to Java, could you explain something to me?
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 6
    Last Post: September 18th, 2011, 07:12 AM
  3. Replies: 1
    Last Post: December 13th, 2010, 04:13 AM
  4. Can anyone explain this code for me
    By gudwindavids in forum Object Oriented Programming
    Replies: 1
    Last Post: December 11th, 2009, 01:29 PM
  5. can anyone explain this?
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 12th, 2009, 02:51 AM