Jason
Jason

Reputation: 421

Question about scope of variables in try/catch block

I have a piece of code that may or may not fail, which I want to try X number of times before giving up. When it fails, it throws a specific Exception, so I figured something like this would work:

    int retries = 0;
    while (retries < MAX_RETRIES) {
        failedFlag = false;
        try {
            //...do some stuff...
            logger.info("After commit...");
            if (!failedFlag) {
                logger.info("Failed flag is false, so breaking out.");
                break;
            }
        } catch (MyException e) {
            retries++;
            failedFlag = true;
            long sleepMillis = MILLIS_TO_SLEEP * retries;
            logger.warn("Caught a failure");
            logger.warn("Will sleep for " + sleepMillis + "msec and then try again.");
            try {
                Thread.sleep(sleepMillis);
                logger.info("Done sleeping...");
                logger.info("Failed flag is " + (failedFlag ? "true" : "false"));
            } catch (InterruptedException e1) {
                logger.warn("Caught interrupted exception while sleeping.  Terminate.");
                transaction.rollback();
                return;
            }
        }
    }

The problem I'm having is that the change made to failedFlag in the first catch block doesn't seem to persist. Upon failure, the thread sleeps, but when it wakes up and re-enters the try block, the failedFlag reverts back to false? I get the following log lines:

2010-09-16 17:09:48,448 WARN  [pool-1-thread-1] synchronizer.FlightCreativeSynchronizer - Will sleep for 60000msec and then try again.
...
2010-09-16 17:10:48,449 INFO  [pool-1-thread-1] synchronizer.FlightCreativeSynchronizer - Done sleeping...
2010-09-16 17:10:48,450 INFO  [pool-1-thread-1] synchronizer.FlightCreativeSynchronizer - Failed flag is true
2010-09-16 17:10:48,453 INFO  [pool-1-thread-1] synchronizer.FlightCreativeSynchronizer - After commit...
2010-09-16 17:10:48,453 INFO  [pool-1-thread-1] synchronizer.FlightCreativeSynchronizer - Failed flag is false, so breaking out.

Kinda boggled here... Are local variables unaffected by changes made in catch blocks?

Upvotes: 2

Views: 793

Answers (5)

aperkins
aperkins

Reputation: 13114

If you put the failed flag above the while loop, it will have a scope outside the loop. Currently, with the scope you have, the flag is redefined every time the loop restarts.

Hope that helps.


Edit: Just to be more clear, you should change

int retries = 0;
while (retries < MAX_RETRIES) {
    failedFlag = false;

to

int retries = 0;
failedFlag = false;
while (retries < MAX_RETRIES) {

Upvotes: 2

Colin Hebert
Colin Hebert

Reputation: 93157

Yes local variables are affected by changes in catch blocks.

And obviously failedFlag goes back to false (at the next iteration), as it prints "Failed flag is false, so breaking out."

Upvotes: 0

zigdon
zigdon

Reputation: 15063

You're setting failedFlag every time you start the loop, so it will always have the false value when you enter the try block. You probably want to initialize it outside of the while loop.

Upvotes: 0

Chris Dodd
Chris Dodd

Reputation: 126175

The first thing you do in the while loop is set the flag to false. So when an exception occurs, you set the flag to true, sleep for a bit, then set the flag to false and retry. Unsuprisingly, after the retry its still false...

Upvotes: 0

letronje
letronje

Reputation: 9148

You are setting failedFlag to false at the top of the while loop.

Upvotes: 4

Related Questions