facebook twitter hatena line email

Flash/as3/error

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

エラー処理の書き方

main.as

import AppError;
public class Main extends Sprite 
  {
  private function init(e:Event = null):void 
  {
    try
    {
      trace("try");
      throw new AppError(AppError.ERR_1);
      trace("end");
    }
    catch (error:AppError)
    {
      trace("catch " + error);
    }
  }
}

AppError.as

package  
{
  /**
   * アプリエラークラス
   */
  public class AppError extends Error 
  {
    static public const ERR_1:uint = 1001;
    public function AppError(code:uint) 
    {
      // メッセージ初期化
      var msg:String = new String();
      switch (code)
      {
      case ERR_1:
        msg = 'エラー1';
        break;
      }
      // メッセージ生成
      var message:String = 'Error:' + code + ' ' + msg;
      super(message);
    }
  }
}

参考

http://livedocs.adobe.com/flex/3_jp/html/help.html?content=11_Handling_errors_08.html