FireworksColdFusionDreamweaverFreehandFlashMXHome
Latest New Content

Latest Free Content
View All
Free Content
Accessibility
CMX Learning Guides
Hosted by enterhost

Understanding Try/Catch in ActionScript

By: Joey Lott

Page 1 of 5

Set for printing

Next

Lot's of people have been asking about how and when to use try/catch functionality in the Flash applications. This article aims to introduce you to the basic concepts of working with try/catch. In this article you'll learn:

Understanding try/catch

The idea with try/catch is that some portion of your code may possibly fail to work, and you want to be able to catch it if it does. It's important to understand what types of errors try/catch can handle, however. It cannot handle syntax errors (that's the compiler's task.) Nor can it handle errors in asynchronous scenarios. For example, you cannot use try/catch methodology to handle an error that may occur should a an XML document fail to parse properly. You can, however, use try/catch to handle errors that are introduced because of invalid parameters, etc.

So then, what are some scenarios in which you could utilize try/catch? Consider the following scenario: You have a function that draws a rectangle such as:

function makeRectangle(nWidth:Number, nHeight:Number):MovieClip {

  // Determine the next depth within the current
  // timeline.
  var nDepth:Number = this.getNextHighestDepth();

  // Create a new movie clip.
  var mcClip:MovieClip = this.createEmptyMovieClip("mcClip" + nDepth, nDepth);

  // Set the linestyle, then draw a filled
  // rectangle with the specified dimensions.
  mcClip.lineStyle(0, 0, 100);
  mcClip.beginFill(0xFFFFFF, 100);
  mcClip.lineTo(nWidth, 0);
  mcClip.lineTo(nWidth, nHeight);
  mcClip.lineTo(0, nHeight);
  mcClip.lineTo(0, 0);
  mcClip.endFill();

  // Return a reference to the movie clip.
  return mcClip;
}

The function works just fine as it stands. However, notice that it doesn't take into account any possible errors that could be introduced because of invalid values for the parameters. For example, if you try to pass in an undefined value, then the function will go on it's merry way thinking that it did what it was asked to do. But obviously, given an undefined value for one or both of the parameters, the function will not be able to create a valid rectangle. One of the options that ActionScript programmers have employed previously was to return different values depending on whether or not an error was encountered. For example, the preceding function could be modified as follows:

function makeRectangle(nWidth:Number, nHeight:Number):MovieClip {

  // Check to see if the width and/or height is
  // undefined. If so, return a value of null.
  if(nWidth == undefined || nHeight == undefined) {
    return null;
  }
  var nDepth:Number = this.getNextHighestDepth();
  var mcClip:MovieClip = this.createEmptyMovieClip("mcClip" + nDepth, nDepth);
  mcClip.lineStyle(0, 0, 100);
  mcClip.beginFill(0xFFFFFF, 100);
  mcClip.lineTo(nWidth, 0);
  mcClip.lineTo(nWidth, nHeight);
  mcClip.lineTo(0, nHeight);
  mcClip.lineTo(0, 0);
  mcClip.endFill();
  return mcClip;
}

Then, you can use the function with an if statement. For example:

function run(nWidth1:Number, nHeight1:Number):Void {

  // Try to create the rectangle.
  var mcRectangle1:MovieClip = makeRectangle(nWidth1, nHeight1);

  // Check to see if the rectangle was created
  // successfully. If not, then display an error
  // message.
  if(mcRectangle1 == null) {
    tOutput.text = "One of the specified dimensions is undefined.";
  }
}

Page 1 of 5 1 2 3 4 5 Next


download
Download Support Files


Keywords
Flash, ActionScript, try, catch, finally, throw, error handling, error checking, Flash Exception Handling