So I have been working on a program in Java. I have a function that runs some code, and when the code throws an exception I want to call it again. Like this:
public void x (String str) {
try {
// Initialize
} catch (SomeException e) {
System.out.println("Initializing again...");
}
try {
// Do stuffz
} catch (SomeOtherException e) {
System.out.println("Trying again...");
x(str);
}
}
This works, but it will throw a stack overflow error if it throws the exception too many times. How can I stop the stack overflow error?
Maybe you could use a wrapper function, i.e. this:
public void wrapper (String str) {
while (true) {
try {
x(str);
} catch (SomeException e1) {
System.out.println("Initializing again...");
continue;
} catch (SomeOtherException e2) {
System.out.println("Trying again...");
continue;
}
}
}