Is error recovery possible in Java?

Consider the following code that generates a StackOverflowError.

public class SimpleProgram {
  static SimpleProgram s = new SimpleProgram();

  public static void main(String[] args) {
      s.f(0);
  }

  void f(int i) {
      System.out.println("f :" + (++i));
      g(i);
  }

  void g(int i) {
      System.out.println("g :" + (++i));
      f(i);
  }
}

It just prints a number from 1 to n ('5417' by f () when executed). After that, it throws a StackOverflowError and exits. Now consider the second program.

public class SimpleProgram{
  static SimplePrograms = new SimpleProgram();

  public static void main(String[] args) {
      s.f(0);
  }

  void f(int i) {
      try {
          System.out.println("f :" + (++i));
          g(i);
      } catch (StackOverflowError e) {
          System.out.println("f :" + (++i));
          g(i);
      }
  }

  void g(int i) {
      try {
          System.out.println("g :" + (++i));
          f(i);
      } catch (StackOverflowError e) {
          System.out.println("g :" + (++i));
          f(i);
      }
  }
}

Now it shows strange behavior. The program does not end as expected, but the displayed values ​​are repeated (say f: 4107 g: 4108 - f: 4120 and again back to f: 4107).

My question is: why is this happening? I thought that an error such as a StackOverflowError means that the current thread stack is full and therefore there is no recovery. The program should stop its forced execution, but does not call the next function, but this did not happen. Can the JVM stack thread grow in size upon request?

+4
2

( System.out.println() f(), g()). StackOverflowError, , , (, , , , ).

( , "catch" println catch):

StackOverflowError , System.out.println(String) println(), g :9662catch g :9663 . - , StackOverflowError, 50 , StackOverflowError, .

... no eception till this point...
g :9660
f :9661
g :9662catch g :9663 
f :9664
g :9665
f :9666
g :9667
f :9668
g :9669
f :9670
g :9671
f :9672
g :9673
f :9674
g :9675
f :9676
g :9677
f :9678
g :9679
f :9680
g :9681
f :9682
g :9683
f :9684
g :9685
f :9686
g :9687
f :9688
g :9689
f :9690
g :9691
f :9692
g :9693
f :9694
g :9695
f :9696
g :9697
f :9698
g :9699
f :9700
g :9701
f :9702
g :9703catch g :9704catch f :9703
g :9704catch g :9705catch g :9702
f :9703
g :9704catch g :9705catch f :9704
g :9705catch g :9706catch f :9701
g :9702
f :9703
g :9704catch g :9705catch f :9704
g :9705catch g :9706catch g :9703
f :9704
g :9705catch g :9706catch f :9705
g :9706catch g :9707catch g :9700
f :9701
g :9702
f :9703
g :9704catch g :9705catch f :9704
g :9705catch g :9706catch g :9703
f :9704
g :9705catch g :9706catch f :9705
g :9706catch g :9707catch f :9702
g :9703
f :9704
g :9705catch g :9706catch f :9705
g :9706catch g :9707catch g :9704
f :9705
....
+2

, , , , , Java . n, .

, , :

main -> f(0)
f(0) -> g(0)
g(0) -> f(1)
f(1) -> g(1)
g(1) -> f(2)
f(2) -> g(2)
g(2) -> f(3) <<== Stack overflow

? . StackOverflowError, :

g(2) handler -> f(3) <<== Qaru #2
f(2) handler -> g(2)
g(2) -> f(3)         <<== Qaru #3
g(2) handler -> f(3) <<== Qaru #4
g(1) handler -> f(2)
f(2) -> g(2)
g(2) -> f(3)         <<== Qaru #5
f(1) handler -> g(1)
g(1) -> f(2)
f(2) -> g(2)
g(2) -> f(3)         <<== Qaru #6
g(0) handler -> f(1)
f(1) -> g(1)
g(1) -> f(2)
f(2) -> g(2)
g(2) -> f(3)         <<== Qaru #7
f(0) handler -> g(0)
g(0) -> f(1)
f(1) -> g(1)
g(1) -> f(2)
f(2) -> g(2)
g(2) -> f(3)         <<== Qaru #8

, , . : , , f g , 2 n n. n .

+2

All Articles