User Guide

Handling synchronous errors in an application 263
throw new ArgumentError("I am an ArgumentError");
}
catch (error:ArgumentError)
{
trace("<ArgumentError> " + error.message);
}
catch (error:Error)
{
trace("<Error> " + error.message);
}
Several methods and properties in the Flash Player API throw run-time errors if they
encounter errors while they execute. For example, the
close() method in the Sound class
throws an IOError if the method is unable to close the audio stream, as demonstrated in the
following code:
var mySound:Sound = new Sound();
try
{
mySound.close();
}
catch (error:IOError)
{
// Error #2029: This URLStream object does not have an open stream.
}
As you become more familiar with the ActionScript 3.0 Language Reference, you’ll notice which
methods throw exceptions, as detailed in each methods description.
The throw statement
Flash Player throws exceptions when it encounters errors in your application at run time. In
addition, you can explicitly throw exceptions yourself using the
throw statement. When
explicitly throwing errors, Adobe recommends that you throw instances of the Error class or
its subclasses. The following code demonstrates a
throw statement that throws an instance of
the Error class,
MyErr, and eventually calls a function, myFunction(), to respond after the
error is thrown:
var MyError:Error = new Error("Encountered an error with the numUsers
value", 99);
var numUsers:uint = 0;
try
{
if (numUsers == 0)
{
trace("numUsers equals 0");
}
}