0
0

Javaで「例外とは」の動作を確認してみた

Posted at

概要

Javaで「例外とは」の動作を確認してみました。
以下のページを参考にしました。

実装

以下のファイルを作成しました。

JSample1_1.java
class JSample1_1{
  public static void main(String args[]){
    int n = 10;

    if (n == 10{
      System.out.println("10です");
    }
  }
}

以下のコマンドを実行しました。

$ javac JSample1_1.java
JSample1_1.java:5: エラー: ')'がありません
    if (n == 10{
               ^
エラー1個

以下のファイルを作成しました。

JSample1_2.java
class JSample1_2{
  public static void main(String args[]){
    int n[] = {18, 29, 36};

    System.out.println("開始します");

    for (int i = 0; i < 4; i++){
      System.out.println(n[i]);
    }

    System.out.println("終了しました");
  }
}

以下のコマンドを実行しました。

$ javac JSample1_2.java 
$ java JSample1_2 
開始します
18
29
36
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
	at JSample1_2.main(JSample1_2.java:8)

まとめ

何かの役に立てばと。

0
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
0
0