Re: Runtime error in Java 6
Daniele Futtorovic wrote:
On 22/07/2008 21:10, Jubert allegedly wrote:
Hello,
This code compiles and runs under Java 5:
public class Java6RunTimeError {
public static void main(String[] args) {
System.out.println("\njust say hello\n");
String main_methodname =
Thread.currentThread().getStackTrace()[2].getClassName();
}
}
It gives a Runtime error under Java 6:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at Java6RunTimeError.main(Java6RunTimeError.java:4)
even when compiled with JDK6. Any clue?
You aren't running this in a console, now are you?
IOW, I doubt this is a difference between 1.5 and 1.6, but rather of
having a (de)bugger or some other layer in-between.
More generally, accessing a stack trace by fixed index, without looking
at its length, has an inherent risk of ArrayIndexOutOfBoundsException.
"Some virtual machines may, under some circumstances, omit one or more
stack frames from the stack trace. In the extreme case, a virtual
machine that has no stack trace information concerning this throwable is
permitted to return a zero-length array from this method."
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Throwable.html#getStackTrace()
Even if starting the same job under the same conditions, the two JREs
could use different numbers of stack frames. That is in addition to the
possibility that the same JRE uses a different number of frames under
different conditions.
Patricia