How to recall a method specified no. of times when exception occurs - java

I tried to re-execute a method specified number of times when exception occurs in a method,
but I am unable re-execute the method
int maxretries=10;
void show(){
try{
display();
}
catch(Exception e)
{
for(int i=1;i<maxretries;i++){
display();//on first retry only I am getting exception
}
}
}
when I run the code it is executed for first retry and I am getting exception but I want to reexecute display() method upto it is excuted successfully with in maximum retries.

The call you coded inside the catch is not inside a try, so it will not catch exceptions.
You need to use other concepts to do this, either calling the whole function again, or coding a successive try block inside the catch (and a further try block inside that catch block, etc.), or coding the loop around the whole try block (probably the best approach).

What about this:
int maxretries = 10;
for (int i = 0; i < maxretries; i++) {
try {
display();
break;
} catch (Exception e) {
// log exception
e.printStackTrace();
}
}

In below program I am executing rerun method specified number of times even though exception occured with 5 sec time gap.
public class ReExecuteMethod {
public static void main(String[] args) throws Exception {
int count = 0;
while (count <= 10) {
try {
rerun();
break;
} catch (NullPointerException e) {
Thread.sleep(5000);
System.out.println(count);
count++;
}
}
System.out.println("Out of the while");
}
static void rerun() {
// throw new NullPointerException();
System.out.println("Hello");
}
}

Related

Java method references that take different numbers of characters?

I'm trying to apply retry logic to a number of methods. For example, I have method1(String) and method2(int, String) that I would like to retry up to a certain number of times.
I would ideally like:
int count = 0;
while (count < MAX_TRIES) {
try {
//run method
} catch (Exception e) {
//increment count
//throw e if count == MAX_TRIES
}
}
inside a method where I could pass in as a parameter method1 or method2. Is there any way to do this? Thanks!
Sure:
public <T> T retry(Callable<T> callable) throws Exception {
int count = 0;
while (true) {
try {
return callable.call();
} catch (Exception e) {
count++;
if (count == MAX_TRIES) {
throw(e);
}
}
}
}
And then
retry(() -> doSomething(a, b));
retry(() -> doSomethingElse(a));
This simple implementation is not very flexible, and could use better exception handling, though. You could use a library to do that (disclaimer: I'm the original author of this library), or at least see how it works and reuse some of its ideas.

Below code should stop after 3 retry and print the final output as false, but it is stuck in the first method call

context :
so the below code needs to be fixed which always enter in the recursion mode and fails to give the final output or do maximum 3 retries
// starting of the main java program
public class RecursiveCall {
public static void main(String[] args) {
Boolean result = callingFirstMethod(0);
System.out.println("Name = "+ result);
}
// calling callingFirstMethod with a variable with tryCount as 0
public static Boolean callingFirstMethod(int tryCount){
final int maxRetries = 3;
boolean successful = false;
// method call which fails in this method and always return false
successful = divisonMethods();
// condition which should be tried only 3 times, need to fix that
while(tryCount < maxRetries && !successful){
try {
Thread.sleep(10000);
} catch (RuntimeException e){
System.out.println("Exception Occured with Timer"+ e.getMessage());
} catch (InterruptedException e) {
e.printStackTrace();
}
// this is where recursion actually starts and in since it always fails and enter in the infinite loop
//and trycount value never increases because in the first call itself it will never complete the loop and never increase the value in the trycount to 2.
successful = callingFirstMethod( tryCount++);
}
return successful;
}
// method which always returns false
public static Boolean divisonMethods(){
if(5>6){
return true;
}else{
return false;
}
}
}
context :
so the below code needs to be fixed which always enter in the recursion mode and fails to give the final output or do maximum 3 retries
I think you do not understand recursion and in that case, it is best to avoid recursion and gather some experience first. It was proved that every recursion can be written as cycle and vice versa.
So in your case, we can just do this:
// starting of the main java program
public class RecursiveCall {
public static void main(String[] args) {
Boolean result = callingFirstMethod(0);
System.out.println("Name = "+ result);
}
// calling callingFirstMethod with a variable with tryCount as 0
public static Boolean callingFirstMethod(int tryCount){
final int maxRetries = 3;
boolean successful = false;
// method call which fails in this method and always return false
successful = divisonMethods();
// condition which should be tried only 3 times, need to fix that
while(tryCount < maxRetries && !successful){
try {
Thread.sleep(10000);
} catch (RuntimeException e){
System.out.println("Exception Occured with Timer"+ e.getMessage());
} catch (InterruptedException e) {
e.printStackTrace();
}
// this is where recursion actually starts and in since it always fails and enter in the infinite loop
//and trycount value never increases because in the first call itself it will never complete the loop and never increase the value in the trycount to 2.
successful = divisonMethods();
tryCount++;
}
return successful;
}
// method which always returns false
public static Boolean divisonMethods(){
if(5>6){
return true;
}else{
return false;
}
}
}
Altough if you insist on recursion, you can. Also the recursion allows you to avoid the while-cycle
// starting of the main java program
public class RecursiveCall {
public static void main(String[] args) {
Boolean result = callingFirstMethod(0);
System.out.println("Name = "+ result);
}
// calling callingFirstMethod with a variable with tryCount as 0
public static Boolean callingFirstMethod(int tryCount){
final int maxRetries = 3;
boolean successful = false;
// method call which fails in this method and always return false
successful = divisonMethods();
// condition which should be tried only 3 times, need to fix that
if (tryCount < maxRetries && !successful){
successful = callingFirstMethod( tryCount + 1);
}
return successful;
}
// method which always returns false
public static Boolean divisonMethods(){
if(5>6){
return true;
}else{
return false;
}
}
}
Be careful when using someVariable++ it will first use the variable and then increment it by 1. Unless you are 100% sure how that works, use this only as standalone command to increase something by +1. Do not use it in complex expressions.

Multi-threading: Program create 10 threads and calls method print thread make sure the output of this method do not interrupt each other

Above prog is working by calling both wait() and join(). Can you tell me which method I should use. Or is there a better way to write this program .Thanks in advance :)
For thread.wait() I create a syncronized block before calling t.start().
public class DisplayThread {
public synchronized void printThread(int threadNumber){
System.out.println("I am thread number: " + threadNumber);
}
}
public class Thread1 extends Thread {
DisplayThread d;
int num;
Thread1(DisplayThread d, int num) {
this.d = d;
this.num = num;
}
public void run() {
d.printThread(num);
}
public static void main(String[] args) {
DisplayThread d = new DisplayThread();
Thread[] t = new Thread[10];
for (int i = 0; i < 10; i++) {
t[i] = new Thread1(d, i);
t[i].start();
try {
t[i].join(); **//t[i].wait(1000) also works fine**
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
If your goal is to have the 10 Threads execute concurrently, you'll need to move the Thread#join call outside of the initial loop.
for (int i = 0; i < 10; i++) {
t[i] = new Thread1(d, i);
t[i].start();
}
for (int i = 0; i < 10; i++) {
try {
t[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Other than that, everything looks fine to me!
Can you tell me which method I should use?
You need to use join() method only by which you are telling the main thread not to start (i.e., the line t[i].start()) the next threads in the iteration. If you don't use join(), the main thread also run parallelly and starts the other threads.
Also, wait() and notify() are meant to solve a different problem i.e., producer/consumer problem, I suggest you look here to understand how this concept works.

How to set a timer in java in a non swing/database application

Is it possible to make a timer which counts down in seconds in a Java console application? So is it possible to say count 10 seconds; print a statement; count another 10 seconds and print another statement etc. If so how could I do it?
// This is how to print with a delay
// Thread.sleep stops the program executoin for an amount of time
System.out.println("String 1");
try {
Thread.sleep(1000); // Thread.sleep takes in milliseconds
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("String 2");
// This is how to count down with a delay. Its accuracy is questionable, because
// printing and increminting k take time, but it should be very close to a second.
for(int k = 10; k > 0; k--)
{
System.out.println(k);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

looping over a try/catch block?

I am trying to write a try catch block like the following, but put it inside a loop. My issue is that when i put it in a while loop, it runs x amount of times. i want it to stop when the first try is successful. but give the option to run up to 3 times.
try {
myDisplayFile();
} catch (FileNotFoundException e1) {
System.out.println("could not connect to that file..");
e1.printStackTrace();
}
public static void myDisplayFile() throws FileNotFoundException{
Scanner kin = new Scanner(System.in);
System.out.print("Enter a file name to read from:\t");
String aFile = kin.nextLine();
kin.close();
Scanner fileData = new Scanner(new File(aFile));
System.out.println("The file " + aFile + " contains the following lines:");
while (fileData.hasNext()){
String line = fileData.next();
System.out.println(line);
}//end while
fileData.close();
}
int max_number_runs = 3;
boolean success = false;
for( int num_try = 0 ; !success && num_try < max_number_runs ; num_try++ )
{
try
{
/* CODE HERE */
success = true;
}
catch( Exception e )
{
}
}
int someCounter = 0;
boolean finished = false;
while(someCounter < 3 && !finished) {
try {
//stuff that might throw exception
finished = true;
} catch (some exception) {
//some exception handling
someCounter++;
}
}
You can break; within a try catch block and it will exit the loop that it's in (not just the try catch block), but from a readability stand point, this posted code might be better.
finished = true should be the final line of the try block. It won't throw an exception. And it will only execute if every other line of the try block executed without an exception. So if you get to finished = true, you didn't throw and exception, so you can toggle your flag and exit the loop.
Otherwise, if an exception is thrown, the finished = true; line won't execute. You'll deal with the exception, then increment the someCounter++ variable.
This is ideal from a readability standpoint because all possible while loop exits are marked in the conditional part of the while loop. The loop will continue until either someCounter is too large, or finished returns true. So if I'm reading your code, all I have to do is look through your loop for the parts where these variables are modified, and I can quickly understand the loop logic, even if I don't yet understand everything the loop is doing. I don't have to hunt for break; statements.
I hope I understood your problem correctly. Here's a way.
int noOfTries = 0;
boolean doneWithMyStuff = false;
final int MAX_LOOP_VAL = 10;
int noOfLoops = 0;
while(noOfTries < 3 && noOfLoops < MAX_LOOP_VAL && !doneWithMyStuff) {
try {
// Do your stuff and check success
doneWithMyStuff = true;
}
catch (Exception e) {
noOfTries++;
e.printStackTrace();
}
finally {
// Close any open connections: file, etc.
}
noOfLoops++;
}

Categories