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.

NetworkComms.SendObject("Message", "127.0.0.1", 10000, "Networking in one line!");

or
connection.SendObject("Message", "Networking in one line!");

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:
try
{
    NetworkComms.SendObject("Message", "127.0.0.1", 10000, "Networking in one line!");
}
catch (CommsException ex)
{
    //A comms exception has occured
    //We catch it here to ensure our application can continue to run

    //We decide what to do here
    //i.e. Try to resend the data, shutdown our application etc

    //In this example we will use the logging features of NetworkComms.Net
    //to write out an error file we can investigate later
    LogTools.LogException(ex, "LogFileName");
}

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):
try
{
    NetworkComms.SendObject("Message", "127.0.0.1", 10000, "Networking in one line!");
}
catch (ConnectionSetupException ex)
{
    //We decide what to do with a ConnectionSetupException here
}
catch (CommunicationException ex)
{
    //We decide to do something differently with a CommunicationException here
}
catch (CommsException ex)
{
    //A comms exception has occured that we have not caught seperately above.

    //In this example we will use the logging features of NetworkComms.Net
    //to write out an error file we can investigate later
    LogTools.LogException(ex, "LogFileName");
}

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.