StackTrace in Flash / ActionScript 3.0

483

Solution 1

As far as I know, the only way to make the stack trace available to your own code is via the getStackTrace() method in the Error class, just like you're already doing. In response to the example in your question, though, I would mention that you don't actually have to throw the Error -- you can just create it and call the method on it:

var tempError:Error = new Error();
var stackTrace:String = tempError.getStackTrace();

Also, like the documentation says, this only works in the debug version of Flash Player, so you can wrap this functionality in an if-block that checks the value of Capabilities.isDebugger if you want.

Solution 2

From Flash Player 11.5 the stack traces are also available in the non-debugger versions of the players as well.

Solution 3

Use the Flex DeBugger (FDB) that comes with the Flex SDK. It's a command-line debugger that allows you to debug .swf, even ones online (if it's a debug version). It allows you to set break-points, print/change variables, and dump the stack, and does not require you to add any extra code. A very useful tool that you shouldn't be without!

The fdb options you will need are 'break' and to specify the class and line where you want execution to halt, and 'bt' or 'info stack' to give you a backtrace of the stack. You can also display almost everything about the application while it runs.

Solution 4

@hasseg is right. You can also preserve the stacktrace information in release version (not debug) by providing the -compiler.verbose-stacktraces=true when compiling your SWF.

Solution 5

I've put together this little function:

public static function getStackTrace() : String
{
    var aStackTrace : Array = new Error().getStackTrace().split("\n");
    aStackTrace.shift();
    aStackTrace.shift();
    return "Stack trace: \n" + aStackTrace.join("\n");
}

I have this function in a custom "Debug" class I use with my apps when developing. The two shift() calls remove the first two lines: The first one is just the string "Error" and the second line refers to this function itself, so it's not useful. You can even remove the third line if you wish (it refers to the line where you place the call to the getStackTrace() function) by adding another shift() call, but I left it to serve as a starting point of the "stack trace".

Share:
483
svkvvenky
Author by

svkvvenky

Updated on July 11, 2020

Comments

  • svkvvenky
    svkvvenky almost 4 years

    I am getting the following error while accessing a DLL from my Java program:

    # #A fatal error has been detected by the Java Runtime Environment:
    
    #  #EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6d9df04a, pid=3696, tid=3244
    
    # #JRE version: 7.0-b147
    # #Java VM: Java HotSpot(TM) Client VM (21.0-b17 mixed mode, sharing windows-x86 )
    # # Problematic frame:
    # # V  [jvm.dll+0x9f04a]
    
    # #Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
    

    Please suggest me some solutions to resolve this. Thanks.

  • Lucas Gabriel Sánchez
    Lucas Gabriel Sánchez over 15 years
    Ok, thanks, i didn't realize that :) But, what a pity that getStackTrace() is the only way to get it.
  • Lucas Gabriel Sánchez
    Lucas Gabriel Sánchez over 15 years
    I want it to debug, not to the release, so there is no problem about that. And thanks again.
  • Joony
    Joony over 13 years
    It's not the only way. See my answer below.
  • hasseg
    hasseg over 13 years
    Thanks Joony, I clarified my answer to say "to make the trace available to your own code" instead of "to get the trace".
  • iND
    iND about 12 years
    With less fuss, you can simply write: var stackTrace:String = (new Error()).getStackTrace(); or even trace("stack trace: " + (new Error()).getStackTrace());.