Let's index the bottom of stack to 0 and subsequent calls (since it is recursive) at stack level 1,2,3 and so on. Each call to workHard will increase the stack index by 1 and each return should decrease the stack length by 1.
Since there isn't any return statement, the piece of code forms an infinite recursion and the only way to come out of this recursion loop is a stack overflow exception that breaks this cycle.
Consider the modified piece of code without try/finally:
public class Workout {
public static void main(String[] args) {
workHard();
System.out.println("It’s nap time.");
}
private static void workHard() {
workHard();
}
}
In the above piece of code, assuming that you got the exception at n'th index of stack. The exception is thrown to n-1th stack which doesn't have any way to handle the exception, hence the exception is thrown to n-2nd stack level and up to 0th level. After getting the exception at the 0th level, a exception is thrown to the main method which too doesn't have any mechanism to handle this exception and an exception is thrown.
Now coming to the code with the try block:
public class Workout {
public static void main(String[] args) {
workHard();
System.out.println("It’s nap time.");
}
private static void workHard() {
try {
workHard();
} finally {
workHard();
}
}
}
Again, at the beginning a recursive call will be first made from the try block and keep on making calls till the n'th level where we get our first stack overflow exception while making a recursive call for the n+1th stack level.
For each stack overflow exception coming from try block, the next line of execution will be the call to workHard in the finally block. At the peak of the stack overflow exception at the n'th level, this exception would be caught and execution goes to the finally block of the n'th level.
Since we already have very limited stack size, this finally block will also throw a stack overflow exception and the exception would be caught at the n-1th stack level. Now see that we have freed one stack level. Now the call in the finally block would again be a success and go till the n'th level before it could throw a stackoverflow exception.
Now we get the stack overflowexception at the n'th stack level. Execution goes to the finally block which again throws an exception and this exception is received in the finally block at the n-1th level. Since there isn't any way to handle the exception, the exception is thrown to the n-2th stack level, which catches the exception and retrigger the recursion.
Note that we have freed two stack levels by now.
Now the recursion level goes till the n'th level again and control returns back to n-3th and reaching again till n'th and returning back to n-4th and so on.
At some time, we will reach the finally block of the 0th stack level and again will go to the n'th level through try and then the finally block before finally throwing the exception to the main method.
The stack is being freed and occupied again and again and hence these many overflow exceptions and time to terminate.
Coming to the second part, will it affect other programs?
No, it shouldn't. The stack size runs out of space out of what was allocated to the JVM. The OS will only allocate limited stack size to the JVM. Which can be also controlled via JVM arguments if required.