Next: 9 Error checking - Up: GNUstep Distributed Objects Previous: 7 A modified client

8 Error checking - timeout exceptions

Up to now, this tutorial has ignored error checking; but to build robust applications, your code needs to be able to manage problems in the network connections, both in the server and in the client.

In this section we examine the simplest problem: when a problem occurs during invocation of a remote method, typically a time-out on the network connection, and an exception is raised. If your client code is to use remote objects in a robust way, you need to be prepared to catch and manage these exceptions in the relevant sections of code.

A timeout means that your client sent the method invocation to the server, but it didn't get a response in a reasonable time. After waiting for 15 seconds (FIXME - this was the old default for gnustep-base, it's probably changed now), the gnustep base library raises a NSPortTimeOutException, which you need to catch if you want to write robust code.

In our example, we need to catch exceptions thrown when getting the file from the FileReader:

NS_DURING
  {
    file = [reader getFile: filename];
  }
NS_HANDLER
  {
    NSLog (@"Got exception while reading file: %@", localException);
    exit (1);
  }
NS_ENDHANDLER
this example is not particularly brilliant because the only thing we do when we catch the exception is printing out the description of the exception and quitting the program - which is the same thing which the gnustep-base library does for us if we don't catch the exception... but it should give you a clear example of how to catch and manage timeouts.


Next: 9 Error checking - Up: GNUstep Distributed Objects Previous: 7 A modified client
2011-02-09