Sometimes when you’re making a Flex application, you’ll expect a certain piece of code to fail at some point. This is the job of try and catch. For example, say I wanted to loop over a bunch of components I had within an HBox:

<mx:HBox id="myHBox">
		<mx:Panel title="1"/>
		<mx:Panel title="2"/>
		<mx:Panel title="3"/>
		<mx:Panel title="4"/>
		<mx:Panel title="5"/>
		<mx:Panel title="6"/>
		<mx:Panel title="7"/>
</mx:HBox>

To loop over these panels without setting a specific id for each, we can use the DisplayObject‘s getChildAt() function. So within the block I could make a function that does the following:

private function averageTitles(box:HBox):Number
				{
				// initialize some variables
				var tempTotal:int = 0;
				var i:int = 0;
				var length:int = 0;
				var isEnd:Boolean = false;
				var tempPanel:Panel;
				//loop until the isEnd variable is true
				while(!isEnd)
					{
					//Try to get the child at location i
					try
						{
						tempPanel = box.getChildAt(i);
						tempTotal += parseInt(tempPanel.title);
						i++;
						}
					//if it fails, we have reached the end of the panels
					catch(e:Error)
						{
						isEnd = true;
						}
					//we always want to increase the length,
					//since the length is longer than the index
					finally
						{
						length++;
						}
					}
				//return the average
				return tempTotal / length;
				}

(Obviously the length is unnecessary, Its just an example)

The try block is what the program will attempt to execute first, and the code you expect to fail. In this example, the myHBox.getChildAt(i) is undoubtedly going to fail at some point when the HBox runs out of panels. When this happens, the program will throw an error, more specifically a RangeError, meaning you’re attempting to access something outside the array’s bounds. The catch block will quite literally “catch” the error before it escapes to the main application and causes a runtime error(the window that pops up when something goes wrong in a flash movie in your browser). The catch block will then execute the code within its block. A catch block can catch many kinds of errors; in my example I use the error type Error, meaning it will catch all errors. Since I know what kind of error will occur in my code, I could have just as easily written:

catch(e:RangeError)
	{
	isEnd = true;
	}

And the effect would be the same. You can also catch multiple types of errors within a try block using multiple catch statements where appropriate.

For a list of all types of errors, look at the Subclasses of the Error class in the Adobe Flex Language Reference.

You may also have noticed the finally block which increments the var length. The finally block is used to execute code regardless of when an error happens. So in the finally block in my code:

finally
	{
	length++;
	}

The length will always be incremented if there is an error or not, as we need the total number of items to find the average.

That’s basically it, I’ll write a more advanced tutorial later on things like throwing new errors and such. Good luck in your coding endeavors.