StackTrace in Flash / ActionScript 3.0

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.