Flash / Flex / ActionScript/Development/Error

Материал из Web эксперт
Перейти к: навигация, поиск

Handling Errors

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        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);
        }
    }
  }
}



Throw an exception from a method

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        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);
        }
    }
    private function displayMessage(message:String):void {
      if(message == undefined) {
        throw new Error("No message was defined.");
      }
      trace(message);
    }
  }
}



Understanding Error Objects: throw new Error();

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var sUsername:String = "";
        try {
          if(sUsername == "") {
            throw new Error("Missing Username.");
          }
        }
        catch (erObject:Error) {
          trace(erObject.message);  // Displays: Missing Username.
        }
    }
  }
}