call stack structure

Discussion in 'Java' started by harryos, Nov 2, 2010.

  1. harryos

    harryos Guest

    hi,

    while going through the java tutorial on Exceptions ,I came across a
    picture of call stack.
    http://download.oracle.com/javase/tutorial/essential/exceptions/definition.html

    In this figure,the called method is shown at the top and caller is at
    bottom.I thought if method A calls B and B calls C,then call stack
    should be like-

    main
    A
    B
    C

    Which one is correct?
    Also in the same tutorial ,on page
    http://download.oracle.com/javase/tutorial/essential/exceptions/declaring.html
    ,there is a statement
    'Sometimes, it's appropriate for code to catch exceptions that can
    occur within it. In other cases, however, it's better to let a method
    further up the call stack handle the exception'
    Is that not contradictory to what was given in the picture?

    regards,
    harry
    harryos, Nov 2, 2010
    #1
    1. Advertising

  2. harryos

    Roedy Green Guest

    On Mon, 1 Nov 2010 21:49:17 -0700 (PDT), harryos
    <> wrote, quoted or indirectly quoted someone
    who said :

    >In this figure,the called method is shown at the top and caller is at
    >bottom.I thought if method A calls B and B calls C,then call stack
    >should be like-


    When the car was invented, the steering wheel could go on either side.
    So what we do, BOTH.

    Same with stack representation, the expression TOP OF STACK suggests
    the most accessible item should be on top. However, stacks grow from
    high addresses down to lower ones in RAM. You just have to look at the
    context to see which convention is being used.

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    Microsoft has a new version out, Windows XP, which according to everybody is the "most reliable Windows ever." To me, this is like saying that asparagus is "the most articulate vegetable ever."
    ~ Dave Barry
    Roedy Green, Nov 2, 2010
    #2
    1. Advertising

  3. harryos

    Tom Anderson Guest

    On Mon, 1 Nov 2010, harryos wrote:

    > while going through the java tutorial on Exceptions ,I came across a
    > picture of call stack.
    > http://download.oracle.com/javase/tutorial/essential/exceptions/definition.html
    >
    > In this figure,the called method is shown at the top and caller is at
    > bottom.I thought if method A calls B and B calls C,then call stack
    > should be like-
    >
    > main
    > A
    > B
    > C
    >
    > Which one is correct?


    Both. Both are ways of representing a structure that exists in a computer;
    both work, neither has any particular claim to being more correct than the
    other.

    > Also in the same tutorial ,on page
    > http://download.oracle.com/javase/tutorial/essential/exceptions/declaring.html
    > ,there is a statement
    > 'Sometimes, it's appropriate for code to catch exceptions that can
    > occur within it. In other cases, however, it's better to let a method
    > further up the call stack handle the exception'
    > Is that not contradictory to what was given in the picture?


    In the tutorial's notation, 'up' is towards the bottom of the page.

    tom

    --
    Scheme is simple and elegant *if you're a computer*.
    Tom Anderson, Nov 2, 2010
    #3
  4. harryos

    Roedy Green Guest

    On Mon, 01 Nov 2010 22:04:28 -0700, Roedy Green
    <> wrote, quoted or indirectly quoted
    someone who said :


    >When the car was invented, the steering wheel could go on either side.
    >So what do we do, BOTH.


    If you are experimenting on paper, you will find it is easier to draw
    changing stacks if your stacks grow down.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    Microsoft has a new version out, Windows XP, which according to everybody is the "most reliable Windows ever." To me, this is like saying that asparagus is "the most articulate vegetable ever."
    ~ Dave Barry
    Roedy Green, Nov 4, 2010
    #4
  5. harryos

    Fred Guest

    On Nov 3, 7:11 pm, Roedy Green <>
    wrote:
    > On Mon, 01 Nov 2010 22:04:28 -0700, Roedy Green
    > <> wrote, quoted or indirectly quoted
    > someone who said :
    >
    > >When the car was invented, the steering wheel could go on either side.
    > >So what do we do, BOTH.

    >
    > If you are experimenting on paper, you will find it is easier to draw
    > changing stacks if your stacks grow down.


    Also, consider when you might want to show only a limited set of the
    callers (it may be hundreds deep). You almost always want to know
    where the exception really occurred. Most debuggers show it as:
    problem in D
    called from C
    called from B
    called from A

    If you want to limit the information to 5 deep, it is easier and
    faster to do it this way, just halting after five outputs. For the
    other direction one would have to unwind the full stack, then discard
    all but the last five (depending on how the 'stack' information is
    kept).
    --
    Fred K
    Fred, Nov 4, 2010
    #5
  6. harryos

    Lew Guest

    On Nov 4, 2:29 pm, Fred <> wrote:
    > On Nov 3, 7:11 pm, Roedy Green <>
    > wrote:
    >
    > > On Mon, 01 Nov 2010 22:04:28 -0700, Roedy Green
    > > <> wrote, quoted or indirectly quoted
    > > someone who said :

    >
    > > >When the car was invented, the steering wheel could go on either side.
    > > >So what do we do, BOTH.

    >
    > > If you are experimenting on paper, you will find it is easier to draw
    > > changing stacks if your stacks grow down.

    >
    > Also, consider when you might want to show only a limited set of the
    > callers (it may be hundreds deep).  You almost always want to know
    > where the exception really occurred. Most debuggers show it as:
    > problem in D
    > called from C
    > called from B
    > called from A
    >
    > If you want to limit the information to 5 deep, it is easier and
    > faster to do it this way, just halting after five outputs. For the
    > other direction one would have to unwind the full stack, then discard
    > all but the last five (depending on how the 'stack' information is
    > kept).
    > --


    When you get the array of 'StackTraceElement' from a 'Throwable':
    <http://download.oracle.com/javase/6/docs/api/java/lang/
    Throwable.html#getStackTrace()>
    "The zeroth element of the array (assuming the array's length is non-
    zero) represents the top of the stack, which is the last method
    invocation in the sequence. Typically, this is the point at which this
    throwable was created and thrown."

    The 'printStackTrace()' method shows the zeroth element at the top of
    the display:
    <http://download.oracle.com/javase/6/docs/api/java/lang/
    Throwable.html#printStackTrace()>
    "the following example may be regarded as typical:
    HighLevelException: MidLevelException: LowLevelException
    at Junk.a(Junk.java:13)
    at Junk.main(Junk.java:4)
    Caused by: MidLevelException: LowLevelException
    at Junk.c(Junk.java:23)
    at Junk.b(Junk.java:17)
    at Junk.a(Junk.java:11)
    ... 1 more
    Caused by: LowLevelException
    at Junk.e(Junk.java:30)
    at Junk.d(Junk.java:27)
    at Junk.c(Junk.java:21)
    ... 3 more"

    It seems that the definition of "top" and "bottom" is situational.

    --
    Lew
    Lew, Nov 4, 2010
    #6
  7. harryos

    Arne Vajhøj Guest

    On 02-11-2010 01:04, Roedy Green wrote:
    > Same with stack representation, the expression TOP OF STACK suggests
    > the most accessible item should be on top. However, stacks grow from
    > high addresses down to lower ones in RAM.


    On most common hardware. It is not given that it has to be so.

    Arne
    Arne Vajhøj, Nov 16, 2010
    #7
    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. Surinder Singh
    Replies:
    1
    Views:
    1,195
    Richard Bos
    Dec 20, 2007
  2. Casey Hawthorne
    Replies:
    3
    Views:
    1,085
    Flash Gordon
    Nov 1, 2009
  3. Debajit Adhikary
    Replies:
    36
    Views:
    2,290
    Andre Kaufmann
    Feb 10, 2011
  4. Sam Roberts
    Replies:
    1
    Views:
    224
    Yukihiro Matsumoto
    Feb 11, 2005
  5. Alok
    Replies:
    3
    Views:
    249
Loading...

Share This Page