LoginSignup
3
0

More than 5 years have passed since last update.

Dart-Exceptions

Posted at

Dart Exceptions

Dart言語に関する例外処理の書き方についてまとめます。
Dart言語はJavaにほとんど似た書き方をしていますが、例外処理に関しては少し独特な書き方をします。
覚えてしまえば見やすいですが、構造を理解することが大切です。

サンプルソースコード

main(List<String> args) {
  try {
    // 例外を発生させる
    throw FormatException("throw exception");
    // 例外を取得する
  } on FormatException catch(e) {
    print(e.message);
  } finally {
    print("finaly");
  }
}
  • 例外を発生させる時は、throw句を使用する。
  • 例外を取得する時は、on句で例外の種類を指定して、catchで例外の変数を定義する。
  • on句を省略してcatchをいきなり書くことが可能
main(List<String> args) {
  try {
    // 例外を発生させる
    throw FormatException("throw exception");
    // 例外を取得する
  } catch(e) {
    print(e.message);
  } finally {
    print("finaly");
  }
}
  • finalyは例外が発生してもしなくても、最後に実行する処理を記載する場所
3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0