Networking by its very nature is asynchronous, i.e. almost anything can happen in any order at any time. This was one of the primary difficulties we have attempted to simplify when writing our network library. We have attempted to keep our network library as transparent as possible when it comes to errors and the aim of this tutorial is to briefly discuss this topic area.

Wherever possible our network library will throw a descriptive exception that you can choose to handle in whatever way you decide. For a complete breakdown please search our API for ‘commsexception class‘. The most common exceptions you may come across are, in no particular order:

  • ConnectionSetupException – An error occurred while the connection was being established.
  • CommunicationException – An error occurred while data was being sent to the destination.
  • ConfirmationTimeoutException – A receive confirmation was not received within the provided timeout.

These can be thrown in a variety of circumstances, e.g. when trying to send objects.

or

To ensure your application does not crash because of an ‘UnhandledException‘ error you need to ensure you catch and act on these exceptions. You can do this in one of two ways. You can either catch all possible exceptions by catching a general ‘CommsException’ as follows:

Or if you want to do something differently depending on the exception type, then as follows (we still catch a general exception at the end in case we have missed something):

You can read more about the try/catch syntax which we have demonstrated here.

When it is not possible to notify the top level application that an error has occurred the library will attempt to decide the most sensible course of action. In most cases this involves closing the connection and/or saving an error log file to the local application directory. If you suspect such problems may be happening you can find our more by enabling logging in your application.

We hope this tutorial covers exception handling in suitable detail. If you feel there is something missing or would like clarification please leave us a comment.