Emmanuel LG
Emmanuel LG

Reputation: 129

How can I handle a specific IOException?

I have a code that uses Socket and SerialPort and I'd like to handle specific exceptions. With Socket I don't have problems, because the SocketException class has SocketErrorCode and I use it in a switch to handle the errors, but I don't find something similar for the IOException class.

Is there a list of exceptions for the IOException class?

try
{
    strTemp = "Command";
    fnWriteCommand(strTemp, communicationInterface);
    index = 10;
}
catch (SocketException socketEx)
{
    switch (socketEx.SocketErrorCode)
    {
        case SocketError.ConnectionRefused:
        case SocketError.ConnectionAborted:
        case SocketError.ConnectionReset:
        case SocketError.NotConnected:
            index = 0; // Restart sequence
            break;
        case SocketError.TimedOut:
            index = 10; // Retry
            break;
    }
}
catch (IOException IOEx)
{
    // Handle specific exceptions
}

Upvotes: 1

Views: 1202

Answers (2)

Cee McSharpface
Cee McSharpface

Reputation: 8726

The System.IO.Ports.SerialPort class uses an internal wrapper around the low-level port communication functions of the operating system, System.IO.Ports.InternalResources.

This module deals with the I/O errors and converts them into a managed System.IO.IOException. Unfortunately, some of them do not use a numeric error code, only a text which is subject to translation (resource identifier and en-us translation):

IO_PortNotFound "The specified port does not exist."
IO_PortNotFoundFileName "The port '{0}' does not exist."
IO_SharingViolation_NoFileName "The process cannot access the port because it is being used by another process."
IO_SharingViolation_File "The process cannot access the port '{0}' because it is being used by another process."

All Winapi HREsult codes that are not mapped to any of those four exception strings, are converted into an IOException using the HResult and text that the Windows API provides via the FormatMessage API.

From the many subclasses of IOException, only EndOfStreamException is used in the serialport context, and can be specifically catched, or queried like this:

if(IOEx is EndOfStreamException) 
{
    var eosex = IOEx as EndOfStreamException;
    ...
}

Upvotes: 1

Rufus L
Rufus L

Reputation: 37020

The IOException class has several derived exceptions that get thrown that give more information about the IO error that was encountered. You can catch these individually:

try { // Some code }
catch (System.IO.DirectoryNotFoundException) { }
catch (System.IO.DriveNotFoundException) { }
catch (System.IO.EndOfStreamException) { }
catch (System.IO.FileLoadException) { }
catch (System.IO.FileNotFoundException) { }
catch (System.IO.InternalBufferOverflowException) { }
catch (System.IO.InvalidDataException) { }
catch (System.IO.PathTooLongException) { }
catch (System.IO.IOException) { // Something else happened. Check InnerException details }

Upvotes: 1

Related Questions