as3——异常处理

 当特定的异常发生时,你想要有计划的捕捉并且用代码处理。
    当发现异常时使用throw语句抛出一个异常。在一个try模块中设置任何可能产生异常的代码,然后用一个或多个相应的catch模块处理可能的异常。
    Flash Player 8.5支持try/catch方法处理ActionScript的异常。就是说万一特定异常发生,你能够写出代码,理性的应付。如果你处理语法的异常(甚至swf不能编译),你可以处理多数异常类型,例如遗漏或无效的数据。
    举个例子说明何时、如何使用try/catch方法:考虑一个根据用户输入的尺寸画出一个长方形的程序。为了画出一个长方形,需要限制用户输入的尺寸。比如,设定有效的数字在1~200之间。当然,你需要在画长方形之前确定数据的有效性,就可以使用try/catch方法。当flash尝试画出长方形时,如果发现尺寸的值是无效的或者超出范围,程序能够智能的处理这个异常。这时候,你可以有多种选择,可以简单的跳过,可以替换成默认值,可以提醒用户输入有效数据。ActionScript处理错误有两个基本工作:抛出异常和捕捉异常。有些异常会自动抛出,像违规操作异常(IllegalOperationError),记忆异常(MemoryError)和指令超时异常(ScriptTimeoutError)。这些包含在flash.errors包中。当错误被检测到时你也可以按自己的习惯抛出这个异常。用throw语句抛出异常。throw的用法是在throw关键词后跟随一个值或要抛出的引用。多数情况是要抛出一个Error对象或Error子类的实例。例如:
throw new Error("A general error occurred.");
    可以看到,Error结构可以接受一个参数,随着这个异常把一条信息给同伴。这个参数是可选的,根据如何处理异常,你可以选择是否利用它。然而多数情况下,它是一个叙述异常原因的信息。也可能为测试异常而纪录的日志。
    一旦有异常抛出,flash将中断当前的进程找到catch模块处理这个异常。任何可能抛出异常的代码必须写在try模块中。当然,如果一个异常抛出,try模块中的代码被终止同时相应的catch模块会执行。下面是一个简单的例子:
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);
}
    测试窗口出现下列信息:
This code is about to throw an error.
The catch block has been called.
The message is: A general error occurred.
    当然,上面的例子过分简单,并不是实际使用的代码,仅仅举例说明的基本程序。能够看到一旦异常抛出,会退出try模块跳到catch模块并且执行了有参数的Error对象。
    更常见的,异常是从函数或方法中抛出的。在flash见到含有throw语句的函数被包含在try模块中。如果这样的话,你也会看到关联的catch模块被执行。然而,如果在函数内的throw语句没有包含在try模块中,flash跳出这个函数寻找下一个包含在try模块中的函数。如果这样,它会中止在try模块中的代码而运行关联的catch模块。在看一个简单的例子:
private function displayMessage(message:String):void {
  if(message == undefined) {
    throw new Error("No message was defined.");
  }
  trace(message);
}

try {
  trace("This code is about to throw an error.");
  displayMessage(  );
  trace("This line won't run");
}
catch (errObject:Error) {
  trace("The catch block has been called.");
  trace("The message is: " + errObject.message);
}
    测试窗口列出下列信息:
This code is about to throw an error.
The catch block has been called.
The message is: No message was defined.
    从输出的内容看,除了throw语句包含在一个函数中而非直接在try模块内,代码的内容和早先的一个例子一样。
    下面的代码举例说明一个比较现实的例子:
// Define a function that draws a rectangle within a specified sprite
private function drawRectangle(sprite:Sprite, newWidth:Number, newHeight:Number):void {

  // Check to see if either of the specified dimensions are not 
  // a number. If so, then thrown an error.
  if(isNaN(newWidth) || isNaN(newHeight)) {
    throw new Error("Invalid dimensions specified.");
  }

  // If no error was thrown, then draw the rectangle.
  sprite.graphics.lineStyle(1, 0, 1);
  sprite.graphics.lineTo(nWidth, 0);
  sprite.graphics.lineTo(nWidth, nHeight);
  sprite.graphics.lineTo(0, nHeight);
  sprite.graphics.lineTo(0, 0);
}

try {

  // Attempt to draw two rectangles within the current sprite. 
  // In this example it is assumed that the variables for the dimensions 
  // are retreiving values from user input, a database, an XML file, 
  // or some other datasource.
  drawRectangle(this, widthA, heightA);
  drawRectangle(this, widthB, heightB);
}
catch(errObject:Error) {

  // If an error occurs, clear any rectangles that were drawn from 
  // the sprite. Then display a message to the user.
  this.graphics.clear(  );
  tOutput.text = "An error occurred: " + errObject.message;
}
    你也能指定一个finally模块附加到try/catch模块中。finally模块内的代码无论是否有异常抛出都会执行。多数情况下不需要finally模块。例如,下面做相同事情的2个例子:
//Without using finally:
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);
  }
  trace("This is the last line displayed.");
}
//With finally:
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.");
  }
}
    然而,无论try和catch模块中发生什么,finally模块都会运行,包括return语句。因此下面的两个函数并不相同:
//Without using finally:
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);
    return;
  }
  // This line won't run if an error is caught.
  trace("This is the last line displayed.");
}
//With finally:
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);
    return;
  }
  finally {
    // This runs, even if an error is caught.
    trace("This is the last line displayed.");
  }
}
    你可以创建更多异常事件的处理体系。在本书中能找到很多适当的异常事件处理体系。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值