例外 – Dart逆引きリファレンス

2012.05.21

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

例外(Exceptions)

例外をスローしたい

throw」キーワードを使用して例外オブジェクトをスローします。

throw new Exception("Intruder Alert!!");

例外をキャッチしたい

try...catch」ステートメントを使用して例外オブジェクトをキャッチします。

try {
  throw new Exception("Intruder Alert!!");
} catch(Exception e) {
  print(e.toString()); // Exception: Intruder Alert!!
}

finally」節も利用可能です。

try {
  Math.parseInt("three");
} catch(BadNumberFormatException bnfe) {
  print("Ouch! Detected: $bnfe");
} catch(var e) {
  print("If some other type of exception");
} finally {
  print("This runs even if an exception is thrown");
}
Ouch! Detected: BadNumberFormatException: 'three'
This runs even if an exception is thrown

お馴染みの記法ですね。(^^)