Flash / Flex / ActionScript/Statement/try catch — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 08:14, 26 мая 2010
Содержание
Catch Blocks Example
package
{
import flash.display.Sprite;
public class Main extends Sprite
{
public function Main()
{
try
{
var n:int = sumSomeNumbers();
trace(n);
} catch (e:UnrelatedError) {
trace("something unrelated happened.");
} catch (e:DispleasingNumberError) {
trace("somewhere, a number is displeasing.");
} catch (e:Error) {
trace("something bad happened");
}
}
protected function sumSomeNumbers():int
{
var sum:int = 0;
for (var i:int = 0; i < 10; i++)
{
sum += someNumber();
}
return sum;
}
protected function someNumber():int
{
var n:int = Math.round(Math.random() * 10);
if (n < 2)
{
throw new PatheticallySmallNumberError();
}
return n;
}
}
}
class DispleasingNumberError extends Error {}
class PatheticallySmallNumberError extends DispleasingNumberError {}
class UnrelatedError extends Error {}
Catching Errors Generated by Flash
package{
import flash.display.*;
public class Main extends Sprite{
public function Main(){
var object:Object = "Definitely Not a Display Object";
try
{
var displayObject:DisplayObject = DisplayObject(object);
} catch (error:TypeError) {
trace("Incompatible cast!");
displayObject = new Sprite();
}
addChild(displayObject);
}
}
}
Multiple throw statements and multiple catch statements
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var sUsername:String = "";
var erToThrow:Error;
try {
if(sUsername == "") {
throw new EmptyStringException();
}
if(sUsername == undefined) {
throw new UndefinedException();
}
}
catch (erObject:EmptyStringException) {
// Code to handle error.
}
catch (erObject:UndefinedException) {
// Code to handle error.
}
}
}
}
Using return in catch
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
changeFlow ( );
}
public function changeFlow ( ):void {
try {
throw new Error("Test error.");
} catch (e:Error) {
trace("Caught: " + e.message);
return;
} finally {
trace("Finally executed.");
}
trace("Last line of function.");
}
}
}
Using return in try, after throw
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
changeFlow ( );
}
public function changeFlow ( ):void {
try {
throw new Error("Test error.");
return;
} catch (e:Error) {
trace("Caught: " + e.message);
} finally {
trace("Finally executed.");
}
trace("Last line of method.");
}
}
}
Working with Basic try/catch Statements
/*
try {
// Code to try.
}
catch (erObject:Error) {
// Code to run if the try code throws an error.
}
*/
package{
import flash.display.Sprite;
public class Main extends Sprite{
public function Main(){
var sUsername:String = "";
try {
if(sUsername == "") {
throw new Error();
}
trace("The try block ran successfully.");
} catch (erObject:Error) {
trace("An error was thrown.");
}
}
}
}