Home » ActionScript 2.0, ActionScript 3.0, AIR, Flash CS4, Flash CS5, Flex

Try-catch statement in actionscript Flash | Try catch Exception handling as3

22 February 2011 No Comment
Share on Facebook

Try catch stament in actionscript used when an exceptional error accours. This article deals with when and how to use Try catch statement in actionscript 3.0 – Flash.

In this article we are going to cover the following topics.

  • try/catch Statement in actionscript 3
  • Basic try/catch Statement Syntax
  • Throwing Exceptions in Actionscript
  • Finally in Actionscript 3
  • conclusion

Previous article about actionscript basics part 6

Note:

The below example code are used in Actionscript 3.0

try/catch Statement in actionscript 3

The try/catch statement encloses some code and is used to handle errors and exceptions that might occur in that code. Exceptions are used in a program to signal that some error or exceptional situation has occurred, and that it doesn’t make sense to continue the program flow until the exception has been handled.

Basic try/catch Statement Syntax:

try {
  trace("This code is about to throw an error.");
}
catch (errObject:Error) {
  trace("The catch block has been called.");
}

Explaination of using try/catch in as3:

Once an error has been thrown, Flash halts the current process and looks for a catch block to handle the error.
This is where the try and catch blocks come into play. Any code that could potentially throw an error should be enclosed in a try block. Then, if an error is thrown, only the code in the try block is halted, and the associated catch block is called.

Throwing Exceptions in Actionscript:

If a method needs to be able to throw an exception, it has to declare the exception(s) thrown in the method signature, and then include a throw-statement in the method. Here is an example:

try {
  trace("This code is about to throw an error.");
  throw new Error("A general error occurred.");
  trace("This line won't run");
}
catch (errObject:Error) {
  trace("The catch block has been called.");
  trace("The message is: " + errObject.message);
}

Finally in Actionscript 3:

You can attach a finally-clause to a try-catch block. The code inside the finally clause will always be executed, even if an exception is thrown from within the try or catch block. If your code has a return statement inside the try or catch block, the code inside the finally-block will get executed before returning from the method. Here is how a finally clause looks:

private function displayMessage(message:String):void {
  try {
    if(message == undefined) {
      throw new Error("The message is undefined.");
    }
    trace(message);
  }
  catch (errObject:Error) {
    trace(errObject.message);
  }
  finally {
    trace("This is the last line displayed.");
  }
}

Conclusion:

You may be wondering where to use try catch finlly statements in Flash actionscript. It actually depends on the situation of our application or code, For example suppose we need a valid user input but user fails: here we need to catch the error and throw the exception and handle it programatically.

Previous article about actionscript basics part 6

GET THE UPDATES VIA EMAIL

We don't share your email anywhere, grab our rss via feedburner


Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.