views:

11

answers:

1

i've created a socket extension class. the pseudo code below tries to make a socket connection using the passed parameters. if there is an security error, such as the port number not being an acceptable port number, i want to handle the security error.

however, i can't catch the security error. a popup window appears with a description of the error and my errorHandler function is never called.

public class mySocket extends Socket
{
private var host:String;
private var port:int;

public function mySocket(host:String, port:int)
     {
     this.host = host;
     this.port = port;

     super();
     init();
     }

public init():void
     {
     addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
     super.connect(host, port);
     }

private function errorHandler(evt:SecurityErrorEvent):void
     {
     trace("Handle Error"); //doesn't get called when security error occurs
     }
}
+1  A: 

You're probably getting a synchronous SecurityError (as opposed to a SecurityErrorEvent, which is asynchronous). The connect method throws this error according to the docs.

If that's the case, you should handle it with a try...catch block.

public init():void
{
    addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler);
    try {
        super.connect(host, port);
    } catch(se:SecurityError) {
        //  handle synchronous security error here
    } catch(ioe:IOError) {
        //  handle synchronous io error here
    }
}
Juan Pablo Califano
ahh! that was it. thanks.
TheDarkInI1978