/******************************************************************************
Code to retry when errored/exceptioned
*******************************************************************************/
class Temp{
public void causeIssue(int i) throws Exception{
if (i != 3){
// try{
throw new OutOfMemoryError("Sample");
// } catch(Exception e){
// throw e;
// }
}
}
public void doSomething() throws Exception{
final int MAX_TRIES = 10;
int count = 0;
while(count++ < MAX_TRIES){
try{
System.out.println("trying");
causeIssue(count); // throws error/exception till count 2
System.out.println("trying successful");
break; // break on success
} catch (Exception e){
System.out.println("caught, logging Exception:" + count);
} catch (Error e){
System.out.println("caught, logging Error:" + count);
}
}
}
}
public class Main
{
public static void main(String[] args) throws Exception {
System.out.println("Hello World");
Temp t = new Temp();
t.doSomething();
}
}