tags:

views:

56

answers:

3

how can i handle stack overflow exception in my game it occures when i play the game plz reply asap possible?

+3  A: 
try
{
    //your work
}
catch(StackOverflowException ex)
{
     // handle it
}
org.life.java
CLR? You mean JVM? :)
bzlm
.WriteLine (or the java equivalent) requires stack space. Is this going to work 100%? I rather doubt it.
Lasse V. Karlsen
@Lasse V. Karlsen @bzlm updated it
org.life.java
Yes, but though this removes the code I was wondering about, it still leaves the question open. What can you possibly do inside that catch-block that is guaranteed to work, that doesn't require stack space?
Lasse V. Karlsen
@Lasse V. Karlsen I think we can just give a alert to user saying something unexpected thing occured ,application is going to close :) we can handle in some case but for that whole scenario should be clear about system
org.life.java
How do you plan on giving that alert? Remember, you can't call any code, you have no stack space. Or perhaps if you catch it low enough on the call stack, the stack has unwinded? I'm sorry, I don't know enough Java to know the answer to that.
Lasse V. Karlsen
we can have an already initialized alert with us, if you create reference then that will be stored in stack, the actual Object resides in heap.
org.life.java
+3  A: 

Well, to be blunt, you should probably not try to handle a Stack Overflow exception.

In some cases, you can't execute code in response to a stack overflow exception, since that code requires stack space, which is unavailable, hence double-fault.

Instead, you should change the code so as to avoid it completely.

This might mean changing your algorithm to some other algorithm, or possibly implementing the stack-based recursion in your own stack and switch to a loop instead.

Lasse V. Karlsen
+1  A: 

Stack overflow is most often related to recursive calls, and not the gc.

GC would throw you an out of memory exception.

public static void main(String args[])
{
        try 
    {
            LongRecursiveMethodOrSomethingLikeThat();
        } catch(StackOverflowError t) {
            // Generic catch// catch(Error e)
            // anything: catch(Throwable t)
            System.out.println("Error:"+t);
            t.printStackTrace();
}
KMan
Is this safe? Has the stack unwinded so that you actually have stack space in the catch block? If not, how can you call anything?
Lasse V. Karlsen
@Lasse V. Karlsen: I understand where you're coming from. Its unlikely the statements in the catch would work. Ideally, when an Stackflow occurs, it should let the app crash.
KMan
Note that I don't know :) I am just asking, in .NET, you cannot assume you have stack space for any calls at all, but in Java, you might, for instance it might unwind the stack back from the point of exception back to the catch block, and thus have freed up enough space to execute code there.
Lasse V. Karlsen
@Lasse V. Karlsen: It may be curable by increasing the stack size, or somehow restarting the VM. Checkout, http://mindprod.com/jgloss/runerrormessages.html#STACKOVERFLOWERROR
KMan
But neither of those solutions are something you can do *from a catch block*.
Lasse V. Karlsen