views:

2650

answers:

3

I want to see the stack trace in any function of my code, so i made somthing like this to call it and print the stack trace:

public function PrintStackTrace() {
    try {
        throw new Error('StackTrace');
    } catch (e:Error) {
        trace(e.getStackTrace());
    }
}

I like to know if there are other way to do this. In some place, the Error class creates the stack trace, but maybe it didn't do it with ActionScript 3.0 so maybe it's not posible, but i want to know.

Thanks!

+9  A: 

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.

hasseg
Ok, thanks, i didn't realize that :)But, what a pity that getStackTrace() is the only way to get it.
unkiwii
I want it to debug, not to the release, so there is no problem about that. And thanks again.
unkiwii
It's not the only way. See my answer below.
Joony
Thanks Joony, I clarified my answer to say _"to make the trace available to your own code"_ instead of _"to get the trace"_.
hasseg
A: 

I figured out you can get all kinds of useful data from a stack trace. Check out this article on how to get the package name, class name, function name and file name from the stack trace. How to get file, package, class, and file name out of a stack trace

Niko Limpika
A: 

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.

Joony