StringIndexOutOfBoundsException
| 日本語 | 文字列範囲外例外 |
| 英語 | string index out of bounds exception |
| ふりがな | すとりんぐいんでっくすあうとおぶばうんずえくせぷしょん |
| フリガナ | ストリングインデックスアウトオブバウンズエクセプション |
J2SEに含まれるクラスのひとつ。パッケージも含めたクラス名はjava.lang.StringIndexOutOfBoundsException。
例外の一種。
Stringクラスにアクセスする際に、インデックスナンバーが文字数より大きい場合やマイナスの場合に投げられる。
IndexOutOfBoundsExceptionクラスのサブクラス。つまり、IndexOutOfBoundsException例外やArrayIndexOutOfBoundsException例外の文字列版である。
RuntimeExceptionクラスのサブクラスであるため、明示的にcatchやthrows指定する必要はない。むしろ、適切なインデックスナンバーを使用していれば決して発生しない例外であり、絶対に発生しないようにしなければならない。決してforループから抜けるために使用してはならない。
例外の一種。
Stringクラスにアクセスする際に、インデックスナンバーが文字数より大きい場合やマイナスの場合に投げられる。
IndexOutOfBoundsExceptionクラスのサブクラス。つまり、IndexOutOfBoundsException例外やArrayIndexOutOfBoundsException例外の文字列版である。
RuntimeExceptionクラスのサブクラスであるため、明示的にcatchやthrows指定する必要はない。むしろ、適切なインデックスナンバーを使用していれば決して発生しない例外であり、絶対に発生しないようにしなければならない。決してforループから抜けるために使用してはならない。
// Sample.java
public class Sample
{
public static void main( String[] args )
{
try
{
String string = "あいうえお";
// charAt()メソッドで、範囲外を指定すると
// StringIndexOutOfBoundsException例外が投げられます。
string.charAt( -1 );
}
catch( StringIndexOutOfBoundsException e )
{
e.printStackTrace();
// java.lang.StringIndexOutOfBoundsException: String index out of range: -1
// at java.lang.String.charAt(String.java:460)
// at Sample.main(Sample.java:11)
}
}
}
public class Sample
{
public static void main( String[] args )
{
try
{
String string = "あいうえお";
// charAt()メソッドで、範囲外を指定すると
// StringIndexOutOfBoundsException例外が投げられます。
string.charAt( -1 );
}
catch( StringIndexOutOfBoundsException e )
{
e.printStackTrace();
// java.lang.StringIndexOutOfBoundsException: String index out of range: -1
// at java.lang.String.charAt(String.java:460)
// at Sample.main(Sample.java:11)
}
}
}
// Sample.java
public class Sample
{
public static void main( String[] args )
{
try
{
String string = "あいうえお";
// charAt()メソッドで、範囲外を指定すると
// StringIndexOutOfBoundsException例外が投げられます。
string.charAt( -1 );
}
catch( StringIndexOutOfBoundsException e )
{
e.printStackTrace();
// java.lang.StringIndexOutOfBoundsException: String index out of range: -1
// at java.lang.String.charAt(String.java:460)
// at Sample.main(Sample.java:11)
}
}
}




