Almas Adilbek
Almas Adilbek

Reputation: 4410

In flash AS3 How to put single try catch code, in order to catch any errors in whole class?

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!

Upvotes: 3

Views: 31458

Answers (3)

sergzach
sergzach

Reputation: 6764

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.

Upvotes: 1

Gus Leo
Gus Leo

Reputation: 67

The best way is using try catch:

try{
  //your command
} catch(e:Error){
  //your command
}

Upvotes: 4

ktutnik
ktutnik

Reputation: 7260

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.

Upvotes: 8

Related Questions