Adding a catch
block like this, and not seeing a stack trace, doesn't indicate that nothing was thrown:
} catch (Throwable t) {
t.printStackTrace();
throw t;
} finally { ... }
Not being able to print the stack trace of a StackOverflowError
immediately after catching a StackOverflowError
is expected: a SOE literally means that no more method calls can be made.
But to invoke printStackTrace()
, you need to make another method call. So, that method call actually results in another, separate StackOverflowError
, which you don't attempt to print. But then the return
in the finally causes the StackOverflowError
to be discarded.
Try this, which doesn't require a method invocation when the StackOverflowError
is thrown:
import java.util.*;
class Main {
public static int cnt = 0;
private static Throwable caught;
public static int blubb() throws Throwable {
try {
cnt++;
return blubb();
} catch (Throwable t) {
caught = t;
throw t;
} finally {
return cnt;
}
}
public static void main(String[] args) throws Throwable {
System.out.println(cnt + ": " + Main.blubb() + ": " + cnt);
System.out.println(caught);
}
}
For me, this prints:
0: 9672: 9672
java.lang.StackOverflowError
So yes, a StackOverflowError
was caught.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…