NEATO! Did you know that stack trace (getStackTrace ) can be used even when NOT throwing an Error to get more actionscript debugging information when you need it (and only when you need it). WITHOUT throwing Errors interrupting program flow and WITHOUT using the sluggish remote debugger.
Plus it's very easy to do. Let's look at example.
if(LID == 0 && n == null && breakCount++ >=0) { //some complicated break condition var er:Error =new Error("BREAK"); //creating but not throwing the error trace(er.getStackTrace()); // see where the issue is happening, but continue running normally! }
In that example we created some conditional checking, created a new Error but only traced the getStackTrace to the Trace console, instead of the normal throwing of the Error to get it.
Why is getStackTrace + Trace approach useful?
Say you are working on a game where a tricky to find bug is happening way into the game at 24fps. You have a general idea of where the error is happening, and that code that is called in multiple times from multiple places, in some cases this works perfectly others are related to a bug you are trying to track down.
There are two tips there: one is to create temporary conditional check once you know the general ball park of the bug. This is useful as breakpoints often don't have the smarts to know one normal condition from an error condition, forcing you spend a lot of time either going from break point to breakpoint or digging through the debugger.
Using the remote debugger isn't always easy or efficient when running say game+physics simulations in the a swf running elsewhere.
So by doing this you get a large stack track, with the exact line numbers on the files, making it much easier to figure out where in the call stack the error is happening evolves.
EXAMPLE OUTPUT:
Error: MY BREAK
at com.someproject.views.model::CharacteModel/setCharacterUI()[C:\src\flash\com\someproject\views\model\CharacteModel.as:381]
at com.someproject.views::CharacterUI/set curCharacterUIModel()[C:\src\flash\com\someproject\views\CharacterUI.as:386]
at com.someproject.views::MainUI/updateView()[C:\src\flash\com\someproject\views\MainUI.as:1399]
at com.someproject.views::MainUI/set currentPerspective()[C:\src\flash\com\someproject\views\MainUI.as:1243]
at com.someproject.views::MainUI/gotoScene()[C:\src\flash\com\someproject\views\MainUI.as:1214]
at com.someproject.views::MainUI/callIN_FromJS()[C:\src\flash\com\someproject\views\MainUI.as:229]
at Function/http://adobe.com/AS3/2006/builtin::apply()
at com.troyworks.events::EventAdapter/callFunction()[C:\DATA_SYNC\CodeProjects\workspace\TroyWorksAS3\dev\src\com\troyworks\events\EventAdapter.as:128]
at Function/http://adobe.com/AS3/2006/builtin::apply()
at flash.external::ExternalInterface$/_callIn()
at ()
NOTE this only works in the flash debug player with you content set to debugging enabled.
I'm not the first to find this
http://weirdcalculator.blogspot.com/2009/04/as3-use-getstacktrace-for-debugging.html
This ultrashock post on getting line numbers has a utility wrapper class to help with this as well as pretty print objects/arrays
{ 1 comment }