ActionScript 3.0 reports problems in two ways. Code that fails while it runs throws
an exception, which try and catch can handle. Work that
finishes later, such as loading a file, reports failure by dispatching an error event.
Where ActionScript 2.0 often failed silently, ActionScript 3.0 throws.
throw
throw expression;
Any value can be thrown, but code normally throws an instance of Error
or a subclass, which carries a message and a stack trace.
if (index < 0 || index >= items.length) {
throw new RangeError("Index " + index + " is out of range");
}
try, catch and finally
try {
statements
} catch (name:ErrorType) {
statements
} [catch (name:OtherType) { … }]…
[finally {
statements
}]
- A
tryneeds at least onecatchor afinally. - Each
catchnames a variable and, optionally, a type. The first clause whose type matches the thrown value runs; the rest are skipped. List the most specific types first andErrorlast. - A
catchwithout a type, or typed*, catches anything. - When no clause matches, the exception goes on to the caller.
finallyruns whether the block finished, threw or returned.
try {
var data:Object = JSON.parse(text);
bytes.readUnsignedInt();
} catch (e:SyntaxError) {
trace("not JSON:", e.message);
} catch (e:EOFError) {
trace("ran out of data");
} catch (e:Error) {
trace("other error", e.errorID, e.message);
} finally {
bytes.position = 0;
}
The Error class
| Member | Meaning |
|---|---|
new Error(message:String = "", id:int = 0) | Creates an error with a message and a number. |
message | The text of the error. |
name | The name of the error class, "Error" by default. |
errorID | The number: the run-time error code for built-in errors, the id argument otherwise. Read-only. |
getStackTrace() | The call stack at the moment the error was created, as a string. Release players up to Flash Player 11.4 and AIR 3.4 return null; later release players give the stack without file names and line numbers, which only debug builds have. |
toString() | The name and the message. |
package com.example {
public class ParseError extends Error {
public var line:int;
public function ParseError(message:String, line:int) {
super(message);
name = "ParseError";
this.line = line;
}
}
}
Built-in error classes
Top level
| Class | Thrown when |
|---|---|
Error | The base class of all of them. |
ArgumentError | The arguments do not match the function, such as the wrong number. |
DefinitionError | A definition already exists. |
EvalError | The Function constructor is used, or eval; neither is supported. |
RangeError | A number is out of its allowed range, such as an index past the end of a vector. |
ReferenceError | A property that does not exist is used on a sealed object, or a variable is not defined. |
SecurityError | The security sandbox forbids the operation. |
SyntaxError | Parsing fails, as in JSON.parse. |
TypeError | A value has the wrong type, including use of null or undefined as an object, and XML that cannot be parsed. |
URIError | A URI function such as decodeURI is given a bad string. |
VerifyError | The bytecode of a SWF is malformed or corrupt. |
flash.errors
| Class | Thrown when |
|---|---|
EOFError | Reading past the end of the data, as in a ByteArray or socket. |
IllegalOperationError | A method is not implemented or not allowed for this object. |
InvalidSWFError | The runtime meets a corrupt SWF file. |
IOError | An input or output operation fails. |
MemoryError | A memory allocation fails. |
ScriptTimeoutError | A script runs past the time limit, 15 seconds by default. |
StackOverflowError | Recursion uses up the stack. |
DRMManagerError | The DRM manager reports a failure. |
PermissionError | The application lacks permission for a resource, such as the camera. |
SQLError | An AIR local database operation fails; SQLErrorOperation holds the names of its operations. |
Error events
Loading, networking and other work that completes later cannot throw into the code that started it. It dispatches an event instead, and a listener handles it. An error event with no listener becomes an uncaught error.
| Event class | Type constant | Dispatched when |
|---|---|---|
flash.events.IOErrorEvent | IOErrorEvent.IO_ERROR | A load or send fails, such as a missing file. |
flash.events.SecurityErrorEvent | SecurityErrorEvent.SECURITY_ERROR | The security sandbox blocks a load or connection. |
flash.events.AsyncErrorEvent | AsyncErrorEvent.ASYNC_ERROR | Code called from native code, such as a NetConnection client method, throws. |
flash.events.ErrorEvent | ErrorEvent.ERROR | The base class; also used directly by some APIs. |
flash.events.UncaughtErrorEvent | UncaughtErrorEvent.UNCAUGHT_ERROR | Any error, thrown or dispatched, that nothing handled (Flash Player 10.1, AIR 2). |
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onLoaded);
loader.addEventListener(IOErrorEvent.IO_ERROR, onFailed);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onFailed);
loader.load(new URLRequest("data.json"));
loaderInfo.uncaughtErrorEvents.addEventListener(
UncaughtErrorEvent.UNCAUGHT_ERROR,
function (e:UncaughtErrorEvent):void {
e.preventDefault(); // stop the debugger dialog
trace("uncaught:", e.error);
});
A debug player shows a dialog for every uncaught error; release players show nothing and carry on with the next event.
Common run-time errors
| Code | Class | Message |
|---|---|---|
| 1006 | TypeError | A value is not a function: something that is not a function was called. |
| 1009 | TypeError | Cannot access a property or method of a null object reference. |
| 1010 | TypeError | A term is undefined and has no properties. |
| 1034 | TypeError | Type Coercion failed: cannot convert a value to a type. |
| 1056 | ReferenceError | Cannot create property on a sealed object. |
| 1063 | ArgumentError | Argument count mismatch. |
| 1065 | ReferenceError | Variable is not defined. |
| 1069 | ReferenceError | Property not found and there is no default value. |
| 1125 | RangeError | The index is out of range. |
| 1502 | ScriptTimeoutError | A script has run for longer than 15 seconds. |
| 2007 | TypeError | A parameter must be non-null. |
| 2025 | ArgumentError | The supplied DisplayObject must be a child of the caller. |
| 2044 | – | Unhandled error event, such as an IOErrorEvent with no listener. |
Adobe’s reference lists every run-time error and compiler error by number.