Flash Player 7.
Instances of the Error object are generated ("thrown") when an error occurs. Typically, instances of the Error object are thrown by the throw statement from within a try..catch..finally block, as shown below.
try {
// In try {} code block, add ActionScript code to execute
// If an error occurs, throw an instance of the Error object
if(someErrorOccured) {
throw new Error();
}
} catch (e) {
// ActionScript to handle error
}
You can also throw custom error objects. To do this, you create a constructor function for the custom error object, and throw a new instance of that object from within the try{} code block.
function CustomErrorObject{
// constructor for CustomErrorObject
}
try {
if(errorCondition) {
throw new CustomErrorObject();
}
} catch (e) {
// Determine the type of error that occurred:
if(e instanceof customErrorObject) {
// handle custom error condition
}
}
| Method | Description |
|---|---|
Error.toString() |
Returns the string representation of an Error object. |
| Property | Description |
|---|---|
Error.message |
A string that contains an error message associated with a particular error. |
Error.name |
The name or exception type of an error. |
Flash Player 7.
new Error([message])
message A string associated with the Error object.
An instance of an Error object.
Constructor; creates a new instance of the Error object. If message is specified, its value is assigned to the object's Error.message property. Typically, new instances of the Error object are thrown by a throw statement and handled ("caught") by a catch statement.
The following example demonstrates how to throw an error by using the throw statement.
var sock = new XMLSocket();
try {
// Try to connect to server, and throw an exception if it fails.
if (!sock.connect("www.yourserver.com", 8080)) {
throw new Error("Couldn't connect socket.");
}
// Otherwise, work with socket object normally...
//
} catch (error) {
if(error.message == "Couldn't connect socket") {
// Inform user that connection failed
status_txt.text = "Connection failed.";
}
}