2

Is there a way to keep looping in a try catch until the exception is fixed?

For example:

try{
//something
}
catch(SomeException e){
  //display error message 'an exception occurred try again'
  //go back to try statement
}

After writing out the above pseudocode, I thought of a possible solution, although I think it going to be frowned upon...would a GOTO statement be okay for this case, if there is no other way? I have never used one and have been taught to stay away from it as much as possible...but this actually seems a case where it can be used. If there is a better way, then please shine some light onto me.

bob dylan
  • 647
  • 1
  • 10
  • 25

1 Answers1

2

Using a loop and retry count strategy you can prevent goto.

Something like:

int MAX_RETRY = 10
int retryCount = 0
while(retryCount < MAX_RETRY){
    try{
        //something
    }
    catch(SomeException e){
        //display error message 'an exception occurred try again'
        //go back to try statement
        retryCount++;
    }
}
Atri
  • 5,511
  • 5
  • 30
  • 40