User Guide
Handling synchronous errors in an application 265
<costCenter>1-234</costCenter>
</EmpCode>;
public function SimpleError()
{
try
{
if (employee.costCenter.length() != 1)
{
throw new Error("Error, employee must have exactly one cost
center assigned.");
}
}
catch (error:Error)
{
var errorMessage:TextField = new TextField();
errorMessage.autoSize = TextFieldAutoSize.LEFT;
errorMessage.textColor = 0xFF0000;
errorMessage.text = error.message;
addChild(errorMessage);
}
}
}
}
Using a wider range of error classes and built-in compiler errors, ActionScript 3.0 offers more
information than previous versions of ActionScript about why something has failed. This
enables you to build more stable applications with better error handling.
Rethrowing errors
When you build applications, there are several occasions in which you may need to rethrow
an error if you are unable to handle the error properly. For example, the following code shows
a nested
try..catch block, which rethrows a custom ApplicationError if the nested catch
block is unable to handle the error:
try
{
try
{
trace("<< try >>");
throw new ArgumentError("some error which will be rethrown");
}
catch (error:ApplicationError)
{
trace("<< catch >> " + error);
trace("<< throw >>");
throw error;