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

Thread: Determening the output when dealing with exceptions without running the code

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Determening the output when dealing with exceptions without running the code

    Hello !

    Consider this code;

     
    void f(int x) {
    	try {
    		g(x);
    		Out.print('e');
    	}catch (E1 e) {
    		Out.print('f');
    	} Out.print('g');
    }
     
    void g(int x) throws E1 {
    	try {
    	 if(x <= 0) throw new E1();
    	 else if( x > 10) throw new E2 ();
    	 Out.print('a'); 
    	} catch (E2 e) {
    		Out.print('b');
    	}finally {
    		Out.print('c');
    	}
    	Out.print('g');
     
    }

    What I want to figure out (without running the code!) is what would be the output of the program when we call the function f in 3 diffrent ways;

    f(0) f(3) (f12)

    Now for f(3) I think the output should be this;

    f(0) = c g e f g
    f(3) = a c g e g
    f(12) = b c g e g

    Now I really really want to do this without having to write the Exception classes and just running the code,that would be cheating and I do not want to cheat.Would you say this is the output for when we pick f to be 0 3 and 12? The part that is troubeling me is the if and else if part.I am not sure when the part after the if else will be executed (the printing of a).

    Looking forward to your answers.

    Thank you!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Determening the output when dealing with exceptions without running the code

    Try playing computer with paper and pencil. "execute" each statement in your mind and write down the results of any changes in variables' values.

    when the part after the if else will be executed
    Looks like it depends on the value in x when it is used in the if statements.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Determening the output when dealing with exceptions without running the code

    Well I already did that,and I've posted what I think would come out for the diffrent values of x.I've redone the task and I think it should be right. What do you think?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Determening the output when dealing with exceptions without running the code

    What do you think?
    Compile and execute the code to see what it produces.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Determening the output when dealing with exceptions without running the code

    I kind of want to avoid doing that,but I guess there is no other way.

    EDIT: Okay so I was right for f(3) and f(12).But for f(0) I was wrong,it should be like this;

    f(0) = c f g

    And I am not sure why that is so.Why is the g at the end of the function g skipped? Why is the e in the function f also skipped?
    Last edited by arhzz; March 13th, 2021 at 03:09 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Determening the output when dealing with exceptions without running the code

    The way I would solve it is to edit the source and add comments on each statement in order as they are executed or skipped.
    For example:
    	 if(x <= 0) throw new E1();  // x=0 throw E1
    	} catch (E2 e) {                   // skip with E1
    	}finally {                      // will always execute
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Determening the output when dealing with exceptions without running the code

    I'll give it a shot,thanks for the suggestion.

Similar Threads

  1. Replies: 4
    Last Post: March 27th, 2014, 09:57 AM
  2. Recognise exceptions when reading code
    By TheCoder in forum Exceptions
    Replies: 2
    Last Post: March 5th, 2013, 08:21 AM
  3. Running with Exceptions
    By Sylis in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 21st, 2012, 06:50 PM
  4. need to know how the code is running
    By Dark knight in forum Java Theory & Questions
    Replies: 3
    Last Post: July 19th, 2012, 09:28 AM
  5. Help with code dealing with parallel arrays.
    By danielp1213 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 13th, 2011, 07:43 PM