ArrayIndexOutOfBoundsException
| 日本語 | 配列範囲外例外 |
| 英語 | array index out of bounds exception |
| ふりがな | あれいいんでっくすあうとおぶばうんずえくせぷしょん |
| フリガナ | アレイインデックスアウトオブバウンズエクセプション |
J2SEに含まれるクラスのひとつ。パッケージも含めたクラス名はjava.lang.ArrayIndexOutOfBoundsException。
例外の一種。配列にアクセスする際に、インデックスナンバーが要素数より大きい場合やマイナスの場合に投げられる。
RuntimeExceptionクラスのサブクラスであるため、明示的にcatchやthrows指定する必要はない。むしろ、適切なインデックスナンバーを使用していれば決して発生しない例外であり、絶対に発生しないようにしなければならない。決してforループから抜けるために使用してはならない。
例外の一種。配列にアクセスする際に、インデックスナンバーが要素数より大きい場合やマイナスの場合に投げられる。
RuntimeExceptionクラスのサブクラスであるため、明示的にcatchやthrows指定する必要はない。むしろ、適切なインデックスナンバーを使用していれば決して発生しない例外であり、絶対に発生しないようにしなければならない。決してforループから抜けるために使用してはならない。
// Sample.java
public class Sample
{
public static void main( String[] args )
{
try
{
// ArrayIndexOutOfBoundsExceptionを投げます。
int[] ints = new int[1];
System.out.println( ints[100] );
}
catch( ArrayIndexOutOfBoundsException e )
{
// 投げられたArrayIndexOutOfBoundsExceptionを拾いました。
e.printStackTrace();
// java.lang.ArrayIndexOutOfBoundsException: 100
// at Sample.main(Sample.java:10)
// ちなみにこのように「使われたインデックスナンバー」も
// 出力されます。
}
try
{
// インデックスナンバーがマイナスでも投げられます。
int[] ints = new int[1];
System.out.println( ints[-1] );
}
catch( ArrayIndexOutOfBoundsException e )
{
// 投げられたArrayIndexOutOfBoundsExceptionを拾いました。
e.printStackTrace();
// java.lang.ArrayIndexOutOfBoundsException: -1
// at Sample.main(Sample.java:26)
}
}
}
public class Sample
{
public static void main( String[] args )
{
try
{
// ArrayIndexOutOfBoundsExceptionを投げます。
int[] ints = new int[1];
System.out.println( ints[100] );
}
catch( ArrayIndexOutOfBoundsException e )
{
// 投げられたArrayIndexOutOfBoundsExceptionを拾いました。
e.printStackTrace();
// java.lang.ArrayIndexOutOfBoundsException: 100
// at Sample.main(Sample.java:10)
// ちなみにこのように「使われたインデックスナンバー」も
// 出力されます。
}
try
{
// インデックスナンバーがマイナスでも投げられます。
int[] ints = new int[1];
System.out.println( ints[-1] );
}
catch( ArrayIndexOutOfBoundsException e )
{
// 投げられたArrayIndexOutOfBoundsExceptionを拾いました。
e.printStackTrace();
// java.lang.ArrayIndexOutOfBoundsException: -1
// at Sample.main(Sample.java:26)
}
}
}
// Sample.java
public class Sample
{
public static void main( String[] args )
{
try
{
// ArrayIndexOutOfBoundsExceptionを投げます。
int[] ints = new int[1];
System.out.println( ints[100] );
}
catch( ArrayIndexOutOfBoundsException e )
{
// 投げられたArrayIndexOutOfBoundsExceptionを拾いました。
e.printStackTrace();
// java.lang.ArrayIndexOutOfBoundsException: 100
// at Sample.main(Sample.java:10)
// ちなみにこのように「使われたインデックスナンバー」も
// 出力されます。
}
try
{
// インデックスナンバーがマイナスでも投げられます。
int[] ints = new int[1];
System.out.println( ints[-1] );
}
catch( ArrayIndexOutOfBoundsException e )
{
// 投げられたArrayIndexOutOfBoundsExceptionを拾いました。
e.printStackTrace();
// java.lang.ArrayIndexOutOfBoundsException: -1
// at Sample.main(Sample.java:26)
}
}
}




