Delaying an exception

I have a method that periodically (e.g. once in every 10 secs) try to connect to a server and read some data from it. The server might not be available all the time. If the server is not available the method throws an exception.

What would be the best way to implement a wrapper method that doesn't throw an exception except if the server wasn't available for at least one minute?

Answers


Keep track of when the last time you successfully reached the server was. If the server throws an exception, catch it and compare to the last time you reached the server. If that time is more than a minute, rethrow the exception.


In pseudocode.

//Create Timer
//Start Timer
bool connected = false;

while (!connected)

    try {
        //Connect To DB

        connected = true;
    }
    catch (Exception ex) {
       if (more than 1 minute has passed)
          throw new Exception(ex);
    }

}

You will have to record the time that you originally try to connect to the server and then catch the exception. if the time that the exception is caught is more than the original time + 1 minute, rethrow the exception. If not, retry.


Ideally you can put a timeout on the call to the server. Failing that do a thread.sleep(600) in the catch block and try it again and fail if the second one doesn't return.


Remember that exception handling is just a very specialized use of the usual "return" system. (For more technical details, read up on "monads".) If the exceptional situation you want to signal does not fit naturally into Java's exception handling system, it may not be appropriate to use exceptions.

You can keep track of error conditions the usual way: Keep a state variable, update it as needed with success/failure info, and respond appropriately as the state changes.


You could have a retry count, and if the desired count (6 in your case) had been met then throw an exception

int count = 0;

CheckServer(count);

public void CheckServer(count) {
try
{
  // connect to server
}
catch(Exception e)
{
 if(count < MAX_ATTEMPTS) {
    // wait 10 seconds
    CheckServer(count++)
  }
  else {
    throw e;
  }
}
}

You can set a boolean variable for whether or not the server connection has succeeded, and check it in your exception handler, like so:

class ServerTester : public Object
{
  private bool failing;
  private ServerConnection serverConnection;
  private Time firstFailure;

  public ServerTester(): failing(false)
  {
  }

  public void TestServer() throws ServerException
  {
    try
    {
       serverConnection.Connect();
       failing = false;
    }
    catch (ServerException e)
    {
      if (failing)
      {
        if (Time::GetTime() - firstFailure > 60)
        {
          failing = false;
          throw e;
        }
      }
      else
      {
        firstFailure = Time::GetTime();
        failing = true;
      }
    }
  }
}

I don't know what the actual time APIs are, since it's been a while since I last used Java. This will do what you ask, but something about it doesn't seem right. Polling for exceptions strikes me as a bit backwards, but since you're dealing with a server, I can't think of any other way off the top of my head.


Need Your Help

Google Cloud Storage Client Library 400 error on devserver as of update 1.8.8

python google-app-engine google-cloud-storage client-library

I haven't changed any code, but when I try to upload an image file using the GCS Client library on app engine's dev server, I am now getting this fatal error: