lundi 31 août 2015

Recursive call in the try-catch statement without StackOverflowError exception

I have the following snippet of code:

public static void main(String[] args) {
    foo();
}
public static void foo() {
    try {
        foo();
    } catch (Throwable t) {
        foo();
    }
}

Can anyone explain me what's going on here? In details, please.


I have changed this piece and added println methods to show something:

...
try {
    System.out.println("+");
    foo();
} catch (Throwable t) {
    System.out.println("-");
    foo();
}
...

I'm getting something like this (the process hasn't stopped):

+
+
+
+
+--
+--
+
+--
+--
+
+
+--
+--
+
+--
+--
+
+
+
+--
+--

I haven't had any exception (like StackOverflowError).



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire