-
Notifications
You must be signed in to change notification settings - Fork 0
Handling exceptions
Handling errors inside the Network Library is very easy. Inside the INetwork interface lies 3 events. Each event provides methods to handle each type of exceptions that might occur inside the Network Library. There are 2 kinds of exceptions. One is just a normal exception that breaks the flow of the Network Library. These are important to handle and do it accordingly. The other type is Warning. If warning occur inside the Library, it will not break the flow of the library. If incoming packets are invalid or cannot be handled, such kinds are handled as Warnings. The final event is basically notification and provides additional debugging information. It lets you know when Network Library encounters situations as well as other things.
Each kind of Exception are found in the NetworkLibrary.Exceptions interface. With that out of the way it's time to show some code sample. this is how you handle lethal exceptions:
theConnection.OnExceptionOccured += new delegateException(theConnection_OnExceptionOccured);
This is basically how you handle all lethal exceptions of the Network Library. Now to add the method:
static void theConnection_OnExceptionOccured(object source, Exception exception)
{
Console.WriteLine("An unknown error occured inside the Network Library: " + exception.ToString());
}
And that's it. That is all you need to do.
Exceptions happen very rarely. Warnings on the other hand usually happen if something weird occurred that cannot be handled default. Usually it's if the programmer made mistakes. The most common mistake that can occur is when you forget to register a type of object before you send it or receive it. To listen on any Warnings that might occur the method is the same as with Exceptions:
theConnection.OnWarningOccured += new delegateWarning(theConnection_OnWarningOccured);
And for the method:
static void theConnection_OnWarningOccured(object source, Warning warning)
{
Console.WriteLine("Warning: " + warning.ToString());
}
Notifications are extra debugging information, usually aimed for the programmer. Notifications happen very often and are usually when Network Library encounters something that it handles in a default manner. In some cases this might not be the desired action and as such, a notification is sent for debug purposes. As always, listening on incoming notifications is done like with Exception and Warning:
theConnection.OnNotificationOccured += new delegateNotification(theConnection_OnNotificationOccured);
The Method is like follows:
static void theConnection_OnNotificationOccured(object source, string message)
{
Console.WriteLine("Debug message: " + message);
}
Now that you know how to handle exceptions and get debugging information you can get head over to the next section, Handling Disconnects.