Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to execute the code in the try block again after an exception in caught in catch block?

Tags:

I want to execute the code in the try block again after an exception is caught. Is that possible somehow?

For Eg:

try {     //execute some code } catch(Exception e) { } 

If the exception is caught I want to go in the try block again to "execute some code" and try again to execute it.

like image 438
Infant Dev Avatar asked Feb 14 '12 05:02

Infant Dev


People also ask

How do I go back to try block after catch?

There is no way of going back from catch to try block. You will have to redesign your code - either wrap each findElement() method in separate try/catch blocks or use a loop as advised in other answers.

What happens after exception is caught?

Because if an exception is thrown, Code in the finally clause will execute as the exception propagates outward, even if the exception aborts the rest of the method execution; Code after the try/catch block will not get executed unless the exception is caught by a catch block and not rethrown.

What will happen if you put try block again inside try block?

When nested try blocks are used, the inner try block is executed first. Any exception thrown in the inner try block is caught in the corresponding catch block. If a matching catch block is not found, then catch block of the outer try block are inspected until all nested try statements are exhausted.

What happens when exception occurs in try block?

When an exception occurs inside a try block, control goes directly to the catch block, so no other code will be executed inside the try block and the value of res will not change.


2 Answers

Put it in a loop. Possibly a while loop around a boolean flag to control when you finally want to exit.

bool tryAgain = true; while(tryAgain){   try{     // execute some code;     // Maybe set tryAgain = false;   }catch(Exception e){     // Or maybe set tryAgain = false; here, depending upon the exception, or saved details from within the try.   } } 

Just be careful to avoid an infinite loop.

A better approach may be to put your "some code" within its own method, then you could call the method from both within the try and the catch as appropriate.

like image 169
ziesemer Avatar answered Oct 11 '22 13:10

ziesemer


If you wrap your block in a method, you can recursively call it

void MyMethod(type arg1, type arg2, int retryNumber = 0) {     try     {         ...     }     catch(Exception e)     {         if (retryNumber < maxRetryNumber)             MyMethod(arg1, arg2, retryNumber+1)         else             throw;     } } 

or you could do it in a loop.

int retries = 0;  while(true) {     try     {         ...         break; // exit the loop if code completes     }     catch(Exception e)     {         if (retries < maxRetries)             retries++;         else             throw;     } } 
like image 39
SynXsiS Avatar answered Oct 11 '22 12:10

SynXsiS