call stack structure

H

harryos

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
 
R

Roedy Green

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
 
T

Tom Anderson

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
 
R

Roedy Green

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
 
F

Fred

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).
 
L

Lew

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.
 
A

Arne Vajhøj

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top