In Flash AS3 I wanna write the single try catch block in order to catch any errors in whole class.
For example, I have a lot of functions in myClass.as. I don't wanna write in each function try catch blocks in order to catch errors in this function.
Is there any methods to do this?
Thank you!

- 4,371
- 10
- 58
- 97
3 Answers
you can't! there is no such easy way like that even in other language either than AS3 except they use AOP approach to do that.
The best practice is just let you classes bubble the Error (Exception) and let the higher layer catch and process the error.
EDIT - regarding coment
Actually the idea is the natural way.. still you need to manually catch every possible error. i'll give you example. Note that the purpose of the example only for clarity between lower layer and higher layer.
for example you have a class in the mid layer (Your Business Process):
public class MyBussiness {
public function loadImages(){
//for example here is a block of method
//possibly throws exception.
}
public function getLoan(){
//lets assume here too
}
}
in the higer layer (I Assume in your View - MXML) you catch the exception like bellow:
var myBussiness:MyBussiness = new MyBussiness():
try {
myBussiness.loadImages();
//any other sequence process
myBussiness.getLoan();
}
catch(error:Error){
//here you process error
//show it to user or make LOG
}
still it can't do a magic like you expect but it is the best practice. Rember only put try catch only on code that has possibility to throw error because try catch is expensive.

- 6,882
- 1
- 29
- 34
-
Or let the higher layer ignore it, too, right? Flash won't display errors if not in debug mode, right? – Dan Rosenstark Aug 11 '10 at 06:24
-
ktutnik, it would be so kind if you can show me small ex to do like that. How can i catch error by higher layer class, which includes myClass.as?! – Almas Adilbek Aug 11 '10 at 06:54
-
Thanks, ktutnik. This is the way i'll implement the catch errors issue. good luck – Almas Adilbek Aug 11 '10 at 07:39
You can catch all not handled errors in your program by using UncaughtErrorEvent
class.
So, may be it what you want. There is an example from the docs (you could place addEventListener
in the constructor of your main class):
package
{
import flash.display.Sprite;
import flash.events.ErrorEvent;
import flash.events.MouseEvent;
import flash.events.UncaughtErrorEvent;
public class UncaughtErrorEventExample extends Sprite
{
public function UncaughtErrorEventExample()
{
loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
drawUI();
}
private function uncaughtErrorHandler(event:UncaughtErrorEvent):void
{
if (event.error is Error)
{
var error:Error = event.error as Error;
// do something with the error
}
else if (event.error is ErrorEvent)
{
var errorEvent:ErrorEvent = event.error as ErrorEvent;
// do something with the error
}
else
{
// a non-Error, non-ErrorEvent type was thrown and uncaught
}
}
private function drawUI():void
{
var btn:Sprite = new Sprite();
btn.graphics.clear();
btn.graphics.beginFill(0xFFCC00);
btn.graphics.drawRect(0, 0, 100, 50);
btn.graphics.endFill();
addChild(btn);
btn.addEventListener(MouseEvent.CLICK, clickHandler);
}
private function clickHandler(event:MouseEvent):void
{
throw new Error("Gak!");
}
}
}
See the docs, please.

- 6,578
- 7
- 46
- 84