There is this code:
public class Main {
public static void main(final String[] args) throws Exception {
System.out.print("1");
doAnything();
System.out.println("2");
}
private static void doAnything() {
try {
doAnything();
} catch (final Error e) {
System.out.print("y");
}
}
}
And there is the output:
1yyyyyyyy2
Why does it print "y" eight times and no more. How can Java call println()
when StackOverflowError
encountered?
My bet is that by invoking
print
in the catch block you force anotherStackOverflowError
that is caught by the outer block. Some of these calls will not have enough stack for actually writing the output stream.The JLS says that: