Exception instead of Compiler Error
I expected this program to give me Compile error but it compiled
successfully and I got a runtime exception while running it.
class substr11
{
public static void main(String args[])
{
String s = "abcde";
System.out.println(s.substring(1,-1));
}
}
The compiler error should be able to parse (string is initialized in the
body itself) and find out that this string operation would result in
compile error right ?. Can someone tell me why it did not throw compile
error ?
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String ind ex out of range: -2 at java.lang.String.substring(Unknown
Source) at substr11.main(substr11.java:9)
No comments:
Post a Comment