Ctech45
Ctech45

Reputation: 496

Resume code in try block after exception is caught

I am fairly new to using try/catch blocks, so I don't know how exactly to perform this command.

If I catch an error, I would like to wait a period of time(10 seconds or so) then try to run the same line of code to attempt to continue in my try block. My program is written in Java. I have looked at both these pages: Page1, Page2, but neither of them are in Java. I have also looked at this, but they are not solving in the using the catch block. Is this possible to do, and how would I implement this in my catch block instead of just printing the error?

Upvotes: 1

Views: 5218

Answers (5)

Patrick J Abare II
Patrick J Abare II

Reputation: 1129

Since you are saying that it is only 2 lines of code that you experience the intermittent error with, try something similar to this.

    public static void main(String... args)
    {
        try
        {
            //Some Logic

            //Error throwing logic in method
            while(!doLogic())
            {
                Thread.sleep(1000);//Sleep here or in doLogic catch
            }

        //Continuing other logic!
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    static Integer i = null;
    public static boolean doLogic()
    {
        try
        {
            //Lines that throw error
            System.out.println(i.toString());//NPE First run
        }
        catch (Exception e)
        {
            i = 1;
            return false;
        }
        return true;
    }

Upvotes: 0

Serhii Soboliev
Serhii Soboliev

Reputation: 840

while(true){
   try{
      //actions where some exception can be thrown
      break;//executed when no exceptions appeared only
   }
   catch(YourException e){
      Thread.sleep(10_000);
   }
}

This cycle will be repeated while you instructions haven't executed. When code in try-block executed succesfully break helps you leave this cycle

Upvotes: 0

deanosaur
deanosaur

Reputation: 611

This simple program loops through array values, testing each until it finds a value that doesn't generate an exception

public static void main(String[] args) {
    int[] array=new int[]{0,0,0,0,5};
    for(int i=0; i<array.length;i++)  {
        try {
            System.out.println(10/array[i]);
            break;
        } catch(Exception e) {
            try { Thread.sleep(1000); } catch(Exception ignore){}
        }
    }
}

Upvotes: 0

JNevens
JNevens

Reputation: 11982

I don't think it is possible to return to a certain line in your try-block from inside a catch-block. Because when the throwis executed, the runtime system is going to pop frames from the call stack, looking for an exception handler to match the thrown exception and once the frame is popped from the stack, it's gone. More info about this can be found here

What you can do is call the method that caused the throw from within the catch-block. But that means it is going to execute your method from the beginning, so maybe you want to try to rearrange your code so that this does not cause any other problems. EDIT: The other answer demonstrates exactly what I mean.

Upvotes: 0

Andrei
Andrei

Reputation: 3106

99% of time, you want to re-run a code-block after a try-catch and not the line with exception.

If you need to run from that line, than that is an indication for you to take your code in another method that encapsulates only that code (maybe move the try-catch there too).

What i would advice is something like this:

void method(){
    try{
       codeline1;
       codeline2;
       codeline3;
       codeline4;
    }
    catch(Exception ex)
    {
        restorClassStateBeforeCodeLine1();
        method();
    }
}

By that snipped i propose to have your entire try-catch in a separate method.

Waiting random intervals is bad practice also. You never know if 10 seconds is right every time or at all.

Another way that I advise against would be:

label: {
    try {
        ...
        if (condition)
            break label;
        ...
    } catch (Exception e) {
        ...
    }
}

It uses java labels to retry that part. I never tried but the break could be moved in the catch and the label in the try.

Upvotes: 1

Related Questions