Code: Select all
class TestException
{
public static void main(String[] args)
{
String str="12345";
char ch = str.charAt(str.length());
}
}
Moderator: admin
Code: Select all
class TestException
{
public static void main(String[] args)
{
String str="12345";
char ch = str.charAt(str.length());
}
}
Could you please let me know what is not clear so that I can explain more?As per the API documentation for charAt, it throws IndexOutOfBoundsException if you pass an invalid value. Both - ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException, extend IndexOutOfBoundsException and although in practice, the charAt method throws StringIndexOutOfBoundsException, it is not a valid option because the implementation is free to throw some other exception as long as it is an IndexOutOfBoundsException.
I find the above explanation very unclear.admin wrote:The given explanation explains exactly what you are asking -Could you please let me know what is not clear so that I can explain more?As per the API documentation for charAt, it throws IndexOutOfBoundsException if you pass an invalid value. Both - ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException, extend IndexOutOfBoundsException and although in practice, the charAt method throws StringIndexOutOfBoundsException, it is not a valid option because the implementation is free to throw some other exception as long as it is an IndexOutOfBoundsException.
HTH,
Paul.
This is not correct. JDBC related methods for columns start indexing with 1!By default java takes 0 for any stating index.
Code: Select all
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
Code: Select all
String s = "abcd";
try {
s.charAt(5);
} catch (IndexOutOfBoundsException ex) {
ex.printStackTrace();
}
Exactly! Standard class! It's available in source code! But it isn't important.admin wrote:String is a standard JDK class. i.e. a class whose source code is *technically* not available to you.
Users browsing this forum: No registered users and 7 guests