Game Development Reference
In-Depth Information
If no error occurs, the code in the catch block will not execute.
You can also use multiple catch blocks to catch different kinds of
errors rather than catching all errors with one lump block.
try {
//ERROR-INDUCING CODE HERE
} catch (error:ArgumentError) {
//CATCHES JUST ARGUMENT ERRORS
trace(error);
} catch (error:ReferenceError) {
//CATCHES JUST REFERENCE ERRORS
trace(error);
} catch (error:Error) {
//CATCHES ALL OTHER ERRORS
trace(error);
}
Ifyouwantsometypeofcodetorunregardlessofwhetheran
error occurs, you can put it in a finally block, which appears after
allcatches.Ifnoerroroccurs,thesequencewillbetry
>
finally. If
an error occurs, it will follow try
>
catch(es)
>
finally.
try {
//ERROR-INDUCING CODE HERE
} catch (error:Error) {
//NOTIFY DEVELOPER OF ERROR
trace(error);
} finally {
trace( " MADE IT THROUGH TO THE END " );
}
Throwing Your Own Errors
Sometimes, you may want to cause errors yourself to let you or
another developer using your code to know that they
re attempting
to perform an illegal operation. Creating an error is known as
throwing to coincide with the catch metaphor. To throw an error,
you simply use the throw statement, which is a core part of AS3.
'
throw new Error(
"
This is a custom error message.
"
);
'
t actually have to specify a message for your error,
particularly if you intend to create your own Error subclass (where
the message could be predetermined by the class), but I find it very
helpful to do so. If you
You don
re working in a complex application that has
a lot of opportunities for errors, you can also define error codes to
provide differentiation as a second parameter to the Error construc-
tor. I don
'
t tend to throw errors as much in my game-specific
classes, but I use them frequently when creating utility classes that
may be shared among a number of projects or other developers.
'
Search Nedrilad ::




Custom Search