User Guide
Types of errors 257
■ Asynchronous errors are run-time errors that occur at various points during run time; they
generate events and are caught by event listeners. An asynchronous operation is one in
which a function initiates an operation, but doesn’t wait for it to complete. You can create
an error event listener to wait for the application or user to try some operation, and if the
operation fails, you catch the error with an event listener and respond to the error event.
Then, the event listener calls an event handler function to respond to the error event in a
useful manner. For example, the event handler could launch a dialog box that prompts the
user to resolve the error.
Consider the file-upload synchronous error example presented earlier. If you successfully
call the
browse() method before beginning a file upload, Flash Player would dispatch
several events. For example, when an upload starts, the
open event is dispatched. When
the file upload operation completes successfully, the
complete event is dispatched.
Because event handling is asynchronous (that is, it does not occur at specific, known,
predesignated times), you need to use the
addEventListener() method to listen for
these specific events, as the following code shows:
var fileRef:FileReference = new FileReference();
fileRef.addEventListener(Event.SELECT, selectHandler);
fileRef.addEventListener(Event.OPEN, openHandler);
fileRef.addEventListener(Event.COMPLETE, completeHandler);
fileRef.browse();
function selectHandler(event:Event):void
{
trace("...select...");
var request:URLRequest = new URLRequest("http://www.yourdomain.com/
fileupload.cfm");
request.method = URLRequestMethod.POST;
event.target.upload(request.url);
}
function openHandler(event:Event):void
{
trace("...open...");
}
function completeHandler(event:Event):void
{
trace("...complete...");
}
For detailed information on asynchronous error handling, see “Responding to error events
and status” on page 267.