Alan Miller
Alan Miller

Reputation: 11

Questions about Java's StackOverflow outputs

When java throws an overflow exception and you start to see red in the output, at exactly what point does the program stop outputting/where is the error message thrown relative to the execution of the line that caused it?

For instance I am running a recursive program and for debugging purposes I am having it print its values every recursion. A stackoverflow error is popping up long before the program stops recursing/printing, but as I follow the output to the bottom the program is doing exactly what I want until it stops printing. Is the error printing when java detects overflow and then the program continues to iterate, or is the last line outputted from my program where the stack overflow occurred?

I know this might be poorly worded, I'm fairly novice and there is probably a much more efficient way to word my question.

Upvotes: 1

Views: 56

Answers (1)

Edwin Buck
Edwin Buck

Reputation: 70909

To prevent an infinite recursion loop, the JVM has a built-in check to see if there are too many nested stack frames (a construct that is mostly related to a function call).

When an exception is thrown, as it moves from the originating stack frame into the higher stack frames, the JVM adds a note to the exception (this is done automatically, behind the scenes) to detail which frame it is leaving and, if a line number table is available, what line number is associated with the operation that threw the exception.

Sometimes an exception is caught, in which case this information about the frames it traveled up will likely never be used (who knows, maybe by now it won't even be generated, the JVM is full of amazing and wondrous optimizations), but should it get to the top level, it is printed and the printing routines know how to look at the exception's stored travel path and will print them out as we now commonly expect.

Since your recursive routines typically travel deeper than the allowed stack-frame stack, you need to rewrite your recursion to not rely upon the stack frame. It just doesn't support the depth you require. To do this, you typically take the data that would be stored in the various levels of the stack frame and put them in a "frame like object". Since it is now in an Object, and not in the frame, you are no longer limited to frame depth, you are limited to available heap memory.

This also means you need to rewrite your algorithm to start with a heap-stored stack. Then it needs to push and pop the correct contexts in the same manner the recursive method would have.

All-in-all it's not a very difficult thing to do, but if you're doing it for the first time, it will seem unfamiliar.

To borrow from another question and condense it a bit, you'll convert an algorithim like

algorithm search(NODE):
  doSomethingWith(NODE)
  for each node CHILD connected to NODE:
    search(CHILD)

to

algorithm search(NODE):
  createStack()
  addNodeToStack(NODE)

  while(stackHasElements)
      NODE = popNodeFromStack()
      doSomethingWith(NODE)
      for each node CHILD connected to NODE:
         addNodeToStack(CHILD)

This will effectively allow you to use the same stack frame to do the entire recursive call, by pushing the recursion onto the heap.

Upvotes: 1

Related Questions