AS: MAIN - dont forget to put it on your christmas list
TRY - CATCH - FINALLY
this is a method of error handling that many of you may not be familiar with. there really is no other necessary knowledge to understand this, except a bit of understanding of classes and conditionals. for all of that, you can look on the AS: MAIN list.
recommended reading
AS: Loops & Conditions by BleeBlap
AS: OOP (Object Oriented Programming) by Inglor
for a bit of background in classes
BASIC TRY/CATCH SETUP
var myNum:Number = null;
try {
if (myNum==null){
throw new Error();
}
}
catch (errorObj:Error){
// put error handling here
}
it should be noted that the try method stops processing when an error is thrown and goes directly to the catch method. now, when you use the try method, it is assumed that it will be followed with the catch method, where you attempt to "throw" an error, that will be "caught" by the catch method. the catch method demands a parameter called the error object. this is used to parse the thrown error. here is a version of the same code, enhanced a bit:
INTERMEDIATE TRY/CATCH SETUP
var myNum:Number = null;
try {
if (myNum==null){
throw new Error("Undefined Variable");
}
}
catch (errorObj:Error){
trace("Unexpected Error: "+errorObj.message);
}
if you didnt follow that code, the output panel will trace "Unexpected Error: Undefined Variable". this is because we threw the error to the catch method, where it parsed it. the error constructor creates a property called "message", where you can create custom errors. you could also try something like throwing different errors with different messages, to intercept special error types. an even better way of intercepting different error types is by extending the error class.
var myNum:Number = Math.round(Math.random());
try {
if (myVar){
throw new TrueVariable();
} else {
throw new FalseVariable();
}
}
catch (errorObj:TrueVariable){
trace("myVar = 1");
}
catch (errorObj:FalseVariable){
trace("myVar = 0");
}
now, we did not properly extend the error class, but the throw method will treat the TrueVariable and the FalseVariable as extensions of the error class, and it will catch in two different ways. the catch with a datatype TrueVariable will only be called if the TrueVariable error is thrown, and a similar thing goes for the FalseVariable error.
ADVANCED TRY/CATCH SETUP
var myWidth:Number = Math.round(Math.random()*25)-5;
var myHeight:Number = Math.round(Math.random()*25)-5;
if (myWidth==0){ myWidth=null };
if (myHeight==0){ myHeight=null };
try {
if (myWidth<=0 || myHeight<=0){
throw new BadDimensions();
} else if (myWidth==undefined || myHeight==undefined){
throw new UndefinedDimensions();
}
_root.createEmptyMovieClip("box", _root.getNextHighestDepth());
with (_root["box"]){
lineStyle(2, 0, 100);
moveTo(0, 0);
lineTo(0, myWidth);
lineTo(myHeight, myWidth);
lineTo(myHeight, 0);
lineTo(0, 0);
}
}
catch (errorObj:BadDimensions){
trace("one of the dimensions of your object are out of range");
}
catch (errorObj:UndefinedDimensions){
trace("one of the dimensions of your object are undefined");
}
this example is a bit large, but two variables are defined randomly between -5 and 25. (i had it change the zeroes to null, just to make this example). if either of the dimensions are undefined (null), it throws an error UndefinedDimensions, which traces an error message. if either of the dimensions are negative, it throws an error BadDimensions, which traces an error message.
FINALLY
this is an optional method, but it runs after the try and catch. you set it up with no parameters, and it is just a way to make sure you run something after the try is completed. usually, this is unneccesary, since whatever is put in the finally block could just as well be put after the whole try/catch routine, but there are some useful methods, such as creating a try/catch/finally routine in a function.
function testFunction():Void {
try {
throw new Error();
}
catch (errObject:Error) {
trace("catch");
return;
}
finally {
trace("finally");
}
}
that will output "catch", and then "finally", despite the fact that the catch block should have aborted the function. other than this, i cant see a reason why the finally couldnt be completely omitted.
that is about it for the try/catch. most of these methods could be combined with OOP to create a more efficient class, but overall, you could possibly achieve the same outcome by just creating a few conditionals. these methods and routines are mostly an issue of readability. they allow you to set up your own error handling system.
SOME PROBLEMS THAT SHOULD BE NOTED:
unfortunately, you cannot make actionscript throw real errors (such as using loadMovieNum or loadMovie in a try block to check for if a file exists). this means that any error handling you do, you must set up for yourself.
that wasnt too long. hope i didnt forget something.