try-catchの、catchを複数書くときの順番
try節の中に例外がおこりうる処理を記述。
catch節にて例外オブジェクトを受け取る。
ArrayIndexOutOfBoundsExceptionとIndexOutOfBoundsExceptionを記述。
上記の順番で書くとコンパイルエラー発生。
ArrayIndexOutOfBoundsException の到達不可能な catch ブロック。すでに IndexOutOfBoundsException の catch ブロックによって処理されています
原因
ArrayIndexOutOfBoundsException はIndexOutOfBoundsException のサブクラス
なので、先にIndex~~~を書いてしまうとそこで例外を受け取ってしまい、
Array~~まで到達しなくなるから。
順番を逆にするとコンパイルエラーはなくなり、
ArrayIndexOutOfBoundsException has occured!!
と出力される。