Results 1 to 3 of 3

Thread: flow controll help!!!

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    flow controll help!!!

    Im taking the Java 2 progammer test in two weeks and one of my weakest points is program flow. Where one has to figure out a small programs output.

    Im a little confused buy this program. The output is supposed to be 4 and 5 but i dont see it at all. If someone could please tell me how they are getting this it would be great.

    class Question{
    public class void main(String[] args){

    for(int i = 0; i <10; ++i){
    try{
    try {
    if(i % 3 == 0) throw new Exception("E0");
    System.out.println(i);
    }catch (Exception inner){
    i *= 2;
    if(i % 3 == 0) throw new Exception("E1");
    }finally{
    ++i;
    }
    }catch (Exception outer) {
    i += 3;
    }finally{
    --i
    }
    }
    }
    }

  2. #2
    VirtuallyVB
    Guest
    First I thought you just needed some rest from studying. Your english typing and Java syntax is off.

    But that is a tricky question. Don't forget that print statements are the basic form of debugging.

    class Question{
    public static void main(String[] args){

    for(int i = 0; i <10; ++i){
    try{
    try {
    if(i % 3 == 0) throw new Exception("E0");
    System.out.println(i);
    }catch (Exception inner){
    i *= 2;
    System.out.println("inner catch top: " + inner);
    if(i % 3 == 0) throw new Exception("E1");
    System.out.println("inner catch bottom: " + inner);
    }finally{
    System.out.println("inner finally");
    ++i;
    }
    }catch (Exception outer) {
    i += 3;
    System.out.println("outer catch: " + outer);
    }finally{
    System.out.println("outer finally");
    --i ;
    }
    }
    }
    }

    produces interesting results:
    inner catch top: java.lang.Exception: E0
    inner finally
    outer catch: java.lang.Exception: E1
    outer finally
    4
    inner finally
    outer finally
    5
    inner finally
    outer finally
    inner catch top: java.lang.Exception: E0
    inner finally
    outer catch: java.lang.Exception: E1
    outer finally

    So you do get your 4 and 5. But I am surprised that it doesn't report that
    System.out.println("inner catch bottom: " + inner);
    is unreachable. Perhaps another value of i (outside of 0-9) reaches it, but I would have expected to see:
    inner catch bottom: java.lang.Exception: E0
    at some point after
    outer catch: java.lang.Exception: E1

    I wouldn't have gotten this one correct.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    "First I thought you just needed some rest from studying. Your english typing and Java syntax is off."

    Not sure what you mean by off. I do need a rest though {{{laughing }}}

    I know that is a hard one and if i see somthing like that on the test im just gonna skip it and go back to it at a later time.

    With the two try statements togther like that
    try{
    try {

    does the first "if" go to the outer catch and the second if go to the inner catch?

    I get:

    1.) first iteneration i = 0
    0 % 3 == 0

    2.)outer catch i += 3
    i = 3

    3.) finally 3 - 1 = 2

    4.) system out 2.

    5.) third iteneration i = 3
    3 % 3 == 0

    6.)outer catch i += 3
    i = 6

    7.) finally 6 - 1 = 5

    8.)system out 5.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width