throw

ユーザー定義の例外を投げます。

構文

throw expression; 
expression
投げる式。

説明

例外を投げるためには throw 文を使ってください。例外を投げるときは、expression で例外の値を指定します。次のそれぞれが例外を投げます:

throw "Error2"; // 文字列値である例外を生成します
throw 42; // 値 42 である例外を生成します
throw true; // 値 true である例外を生成します

例: オブジェクトを投げる

例外を投げるときにオブジェクトを指定することができます。そうすれば、catch ブロックの中でそのオブジェクトのプロパティを参照できます。次の例は、UserException 型のオブジェクト myUserException を生成し、それを throw 文の中で使っています。

function UserException(message) {
   this.message = message;
   this.name = "UserException";
}
function getMonthName(mo) {
   mo = mo-1; // 配列の添え字のために月の数を調整する (1=Jan, 12=Dec)
   var months = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
      "Aug", "Sep", "Oct", "Nov", "Dec");
   if (months[mo] != null) {
      return months[mo];
   } else {
      myUserException = new UserException("InvalidMonthNo");
      throw myUserException;
   }
}

try {
   // 試みる文
   monthName = getMonthName(myMonth);
} catch (e) {
   monthName = "unknown";
   logMyErrors(e.message, e.name); // エラーハンドラに例外オブジェクトを渡す
}

例: オブジェクトを投げるもうひとつの例

次の例では入力文字列で U.S. zip コードのテストをします。もし zip コードが無効な書式を使っていたなら、throw 文は ZipCodeFormatException 型のオブジェクトを生成して例外を投げます。

/*
 * ZipCode オブジェクトを生成します。
 *
 * zip コードとして受け入れられる書式は次のとおりです:
 *    12345
 *    12345-6789
 *    123456789
 *    12345 6789
 *
 * もし ZipCode コンストラクタに渡された引数が、これらのパターンの
 * うちひとつにも確かめられないのなら、例外が投げられます。
 */

function ZipCode(zip) {
   zip = new String(zip);
   pattern = /[0-9]{5}([- ]?[0-9]{4})?/;
   if (pattern.test(zip)) {
      // zip コードの値は文字列中の最初の一致部分です
      this.value = zip.match(pattern)[0];
      this.valueOf = function() {
         return this.value
      };
      this.toString = function() {
         return String(this.value)
      };
   } else {
      throw new ZipCodeFormatException(zip);
   }
}

function ZipCodeFormatException(value) {
   this.value = value;
   this.message = "does not conform to the expected format for a zip code";
   this.toString = function() {
      return this.value + this.message
   };
}

/*
 * これは、US の住所のためのアドレスデータを検証するスクリプトで
 * 使われるかもしれません。
 */

var ZIPCODE_INVALID = -1;
var ZIPCODE_UNKNOWN_ERROR = -2;

function verifyZipCode(z) {
   try {
      z = new ZipCode(z);
   } catch (e) {
      if (e instanceof ZipCodeFormatException) {
         return ZIPCODE_INVALID;
      } else {
         return ZIPCODE_UNKNOWN_ERROR;
      }
   }
   return z;
}

a = verifyZipCode(95060);         // 95060 を返します
b = verifyZipCode(9560;)          // -1 を返します
c = verifyZipCode("a");           // -1 を返します
d = verifyZipCode("95060");       // 95060 を返します
e = verifyZipCode("95060 1234");  // 95060 1234 を返します

例: 例外を再度投げる

例外を捕捉した後、その例外を再度投げるために throw を使うことができます。次の例では、数値である例外を捕捉し、もしその値が 50 を超えるのなら、それを再度投げます。再度投げられた例外は、利用者がわかるように、囲んでいる関数またはトップレベルにいたるまで伝播します。

try {
   throw n; // 数値である例外を投げる
} catch (e) {
   if (e <= 50) {
      // 1 から 50 の例外を操作するための文
   } else {
      // この例外を操作できないので、再度投げる
      throw e;
   }
}

参照

try...catch

ドキュメントのタグと貢献者

Contributors to this page: teoli, ethertank, Mgjbot, Nanto vi
最終更新者: teoli,
サイドバーを隠す