Try {//... Try block...} Finally {//... Finally block...}
Try {//... Try block...}
Catch (Error[: Errortype1])//... Catch Block...}
[Catch (Error[: Errortypen]) {//... Catch Block...}]
[Finally {//... Finally block...}]
Contains a code block. If an error occurs in the code block, the error is returned. Iftry
Any code in the code block throws an error (usingthrow
Statement), the control will be passedcatch
Block (if any), and then passfinally
Code block (if any ). Whether an error is thrown or not,finally
The code block will always be executed. Iftry
The code in the code block does not throw an error (that is, iftry
Code block is successfully completed ),finally
The code in the code block is still executed. Even iftry
Code block usagereturn
Statement exit,finally
The code block will still be executed.
try
The code block must be followed bycatch
Code block,finally
Code block, or both. Onetry
Multiple code blockscatch
Code block, but only onefinally
Code block. You can nest any layertry
Code block.
catch
TheErrorThe parameter must be a simple identifier, suche
,theException
Orx
. You can alsocatch
The variable in the processing function specifies the type. When there are multiplecatch
When a code block is used together, you can capture errors of the type fromtry
Multiple types of errors thrown in the code block.
If the thrown exception is an object, the type will match when the thrown object is a subclass of the specified type. If the thrown error belongs to a specific typecatch
The code block is executed. If the thrown exception does not belong to the specified typecatch
Code block, but the exception is automatically removed fromtry
The code block is thrown and thrown to the matchedcatch
Processing functions.
If an error is thrown in a function and the function does not containcatch
Processing function, the ActionScript interpreter will exit the function and any caller function untilcatch
Code block. In this processfinally
Processing functions.
Availability:ActionScript 1.0; Flash Lite 2.0
Parameters
Error:
Object -- fromthrow
The expression thrown by a statement, usually an error class or an instance of a subclass of it.
Example
The following example shows how to createtry..finally
Statement. Becausefinally
The code in the code block is certainly executed. Therefore, it is usually used intry
After the code block is executed, perform any necessary cleanup. In the following example,setInterval()
Call the callback function every 1000 milliseconds (1 second. If an error occurs, an error is thrown.catch
Code block capture. Whether an error occurs or not, the finally code block is executed. BecausesetInterval()
, SoclearInterval()
Must be placed infinally
Block to ensure that the interval is cleared from memory.
myFunction = function () { trace("this is myFunction"); }; try { myInterval = setInterval(this, "myFunction", 1000); throw new Error("my error"); } catch (myError:Error) { trace("error caught: "+myError); } finally { clearInterval(myInterval); trace("error is cleared"); }
In the following example,finally
The code block is used to delete an ActionScript object, regardless of whether an error occurs. Create a new as file named account..
class Account { var balance:Number = 1000; function getAccountInfo():Number { return (Math.round(Math.random() * 10) % 2); } }
In the directory where account. As is located, create a new as or FLA document and enter the following ActionScript in frame 1st of the timeline:
import Account; var account:Account = new Account();try { var returnVal = account.getAccountInfo(); if (returnVal != 0) { throw new Error("Error getting account information."); } } finally { if (account != null) { delete account; } }
The following exampletry..catch
Statement. Runtry
Code in the code block. Iftry
If any code in the code block throws an exception, the control is passedcatch
Code block.Error.toString()
Method to display an error message in a text field.
In the directory where account. As is located, create a new FLA document and enter the following ActionScript in frame 1st of the timeline:
import Account; var account:Account = new Account(); try { var returnVal = account.getAccountInfo(); if (returnVal != 0) { throw new Error("Error getting account information."); } trace("success"); } catch (e) { this.createTextField("status_txt", this.getNextHighestDepth(), 0, 0, 100, 22); status_txt.autoSize = true; status_txt.text = e.toString(); }
The following example illustratestry
Code block, which contains multiple SPECIFIED TYPEScatch
Code block. According to the error type,try
The code block throws different types of objects. In this example,myRecordSet
Is an instance of a class named recordset (assuming there is such a class ).sortRows()
Methods can throw two types of errors: recordsetexception and malformedrecord.
In the following example, the recordsetexception and malformedrecord objects are subclasses of the error class. They are all defined in their own as class files.
// In recordsetexception. as: Class recordsetexception extends error {var message = "record set exception occurred. ";} // In malformedrecord. as: Class malformedrecord extends error {var message = "Malformed Record exception occurred. ";}
In the recordset classsortRows()
Method. The following example shows the appearance of the Code:
class RecordSet { function sortRows() { var returnVal:Number = randomNum(); if (returnVal == 1) { throw new RecordSetException(); } else if (returnVal == 2) { throw new MalformedRecord(); } } function randomNum():Number { return Math.round(Math.random() * 10) % 3; }}
Finally, in another as file or FLA script, the following code callssortRows()
Method. It targetssortRows()
Each type of error thrown defines the correspondingcatch
Code block
import RecordSet; var myRecordSet:RecordSet = new RecordSet();try { myRecordSet.sortRows(); trace("everything is fine"); } catch (e:RecordSetException) { trace(e.toString()); } catch (e:MalformedRecord) { trace(e.toString()); }