Java exceptions using recursive method

Discussion in 'Java' started by Rodusa, Apr 23, 2008.

  1. Rodusa

    Rodusa Guest

    I am a beginner in java and I am having difficulty in understanding
    the output below which uses exceptions and recursion. So far I was
    able to understand the logic until line 10. After that I am lost. I
    don't understand why is i being decremented? first it printed rec 1,
    rec 2 , rec 3 and when it reached the exception it started to
    decrement i like 3, 2, 1.

    Rod

    Output:

    1. A(int)
    2. B(int)
    3. A()
    4. B()
    5. C()
    6. 2
    7. rec 1
    8. rec 2
    9. rec 3
    10. Foo excepted
    11. Handled in C -> i:3
    12. Finally
    13. Handled in C -> i:2
    14. Finally
    15. Handled in C -> i:1
    16. Finally
    17. Handled in Main

    class A{
    int x;
    public A(){
    x=2;
    System.out.println("A()");
    }

    public A(int x){
    this.x = x;
    System.out.println("A(int)");
    }

    public void print(){
    System.out.println(x);
    }
    }

    class B extends A{
    int x;
    public B(){
    x=5;
    System.out.println("B()");
    }

    public B(int x){
    super(x);
    x=5;
    System.out.println("B(int)");
    }

    public void foo() throws Exception{
    System.out.println("Foo excepted");
    throw new Exception();

    }
    }

    class C extends B{
    int x;
    public C(){
    x=1;
    System.out.println("C()");
    }

    public void rec(int i) throws Exception{
    System.out.println("rec " + i);
    try{
    if(i==3)
    foo();
    else
    rec(i+1);
    } catch(Exception e){
    System.out.println("Handled in C -> i:" +i);
    throw e;
    } finally {
    System.out.println("Finally");
    }
    }
    }

    class Main{
    public static void main(String[] args){
    try{
    B b = new B(3);
    C c = new C();
    c.print();
    c.rec(1);
    } catch (Exception e){
    System.out.println("Handled in Main");
    }
    }
    }
    Rodusa, Apr 23, 2008
    #1
    1. Advertising

  2. Rodusa wrote:
    > I am a beginner in java and I am having difficulty in understanding
    > the output below which uses exceptions and recursion. So far I was
    > able to understand the logic until line 10. After that I am lost. I
    > don't understand why is i being decremented? first it printed rec 1,
    > rec 2 , rec 3 and when it reached the exception it started to
    > decrement i like 3, 2, 1.
    >
    > Rod
    >
    > Output:
    >
    > 1. A(int)
    > 2. B(int)
    > 3. A()
    > 4. B()
    > 5. C()
    > 6. 2
    > 7. rec 1
    > 8. rec 2
    > 9. rec 3
    > 10. Foo excepted
    > 11. Handled in C -> i:3
    > 12. Finally
    > 13. Handled in C -> i:2
    > 14. Finally
    > 15. Handled in C -> i:1
    > 16. Finally
    > 17. Handled in Main

    ....
    > public void rec(int i) throws Exception{
    > System.out.println("rec " + i);
    > try{
    > if(i==3)
    > foo();
    > else
    > rec(i+1);
    > } catch(Exception e){
    > System.out.println("Handled in C -> i:" +i);
    > throw e;
    > } finally {
    > System.out.println("Finally");
    > }
    > }

    ....

    The stack can contain several invocations of rec, each with its own
    value of i. Nothing is decrementing i, you are just getting reports from
    older rec invocations.

    Patricia
    Patricia Shanahan, Apr 23, 2008
    #2
    1. Advertising

  3. Rodusa

    Rodusa Guest

    But until line 10 there was no exception which means that it can only
    encounter one stop point where(i==3). At this point where foo is
    called and execution is returned back to catch exception of rec
    shouldn't the program just terminate at line 12. I don't get the
    following lines?

    Thanks

    Rod
    Rodusa, Apr 23, 2008
    #3
  4. Rodusa

    Rodusa Guest

    Patricia. I have deitel 7th edition but it is huge and too detailed. I
    would like something simpler just to get the concepts.

    You're right I think my problems is that I don't understand quite well
    exceptions. Maybe because the recursive invocations that you're
    talking about is what is making it difficult to understand.
    For example why does Rec have to explicitly specify that it throws
    Exception if it is going to throw anyways....?

    Another thing I don't understand is why specify throw e if it is going
    to jump to finally anyways?
    System.out.println("Handled in C -> i:" +i);
    throw e;

    thank you

    Rodrigo
    Rodusa, Apr 23, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa
    Replies:
    5
    Views:
    30,012
    Chris Smith
    Jul 14, 2004
  2. Paul Miller
    Replies:
    3
    Views:
    1,014
    Alex Martelli
    Nov 12, 2003
  3. Replies:
    3
    Views:
    607
    Sherm Pendley
    Apr 16, 2007
  4. n00m
    Replies:
    12
    Views:
    1,111
  5. vamsi
    Replies:
    21
    Views:
    2,069
    Keith Thompson
    Mar 9, 2009
Loading...

Share This Page