User Guide
264 Handling Errors
catch (error:uint)
{
throw MyError; // Catch unsigned integer errors.
}
catch (error:int)
{
throw MyError; // Catch integer errors.
}
catch (error:Number)
{
throw MyError; // Catch number errors.
}
catch (error:*)
{
throw MyError; // Catch any other error.
}
finally
{
myFunction(); // Perform any necessary cleanup here.
}
Notice that the catch statements are ordered so that the most specific data types are listed
first. If the
catch statement for the Number data type was listed first, neither the catch
statement for the uint data type nor the catch statement for the int data type would ever get
executed.
Displaying a simple error message
One of the biggest benefits of the new exception and error event model is that it allows you to
tell users when and why an action has failed. Your part is to write the code to display the
message and offer options in response.
The following code shows a simple
try..catch statement to display the error in a text field:
package
{
import flash.display.Sprite;
import flash.text.TextField;
public class SimpleError extends Sprite
{
public var employee:XML =
<EmpCode>
<costCenter>1234</costCenter>
NOTE
In the Java programming language, each function that can throw an exception must
declare this fact, listing the exception classes it can throw in a
throws clause attached to
the function declaration. ActionScript does not require you to declare the exceptions
that can be thrown by a function.