I was trying to "intentionally" throw an error, by trying to access wrong URL as follows :
try
{
var myData:URLRequest = new URLRequest("http://www.abc.com/");
myData.method = URLRequestMethod.POST;
var vars:URLVariables = new URLVariables();
var loader:URLLoader = new URLLoader();
loader.load(myData);
}
catch(e:Error)
{
trace("Yess!, caught it!");
}
The above code, couldnot catch any error!
Later i found that, there are two packages as follows :
flash.errors.IOError;
flash.events.IOErrorEvent;
so i figured out that it should have an addEventListener as follows :
loader.addEventListener(IOErrorEvent.IO_ERROR, errorOccured);
But it left me wondering, why i have to listen an event for an error. Why can’t i just make use of try and catch. Why two ways of error handling, one by try-catch another by listening an event? .
Thanks
V.
You can’t use a try-catch on the URLLoader load method because the load method works asynchronously; Code execution does not suspend until the download finishes. This is typically how the Flash Player engine works. Asynchronous methods that can fail will dispatch error events, while synchronous methods that can fail will throw exceptions.