How to distinguish programmatically between different IOExceptions?
Asked Answered
T

1

10

I am doing some exception handling for code which is writing to the StandardInput stream of a Process object. The Process is kind of like the unix head command; it reads only part of it's input stream. When the process dies, the writing thread fails with:

IOException
The pipe has been ended. (Exception from HRESULT: 0x8007006D)

I would like to catch this exception and let it fail gracefully, since this is expected behavior. However, it's not obvious to me how this can robustly be distinguished from other IOExceptions. I could use message, but it's my understanding that these are localized and thus this might not work on all platforms. I could also use HRESULT, but I can't find any documentation that specifies that this HRESULT applies only to this particular error. What is the best way of doing this?

Touchline answered 21/7, 2014 at 23:35 Comment(6)
What is the type of the stream you're trying to write to? Could you possibly query the stream to see if it is closed and if so fail silently?Artemas
Don't you know which code is running when the exception is seen? Put a try/catch around that specific code, then handle whichever exception you receive.Quotha
@JohnSaunders the problem is that I can't distinguish one type of IOException from another. I know where to put the try-catch, but not how to distinguish the right exception in the catchTouchline
How many different IOExceptions occur while calling the one method that receives this exception? In any case, if you put the try/catch immediately around the one line that receives the exception, and if you include catch blocks for exceptions derived from IOException, then you'll simply have to ignore all the remaining cases of IOException.Quotha
@JohnSaunders: I honestly don't know how many different types of IOExceptions could occur. It seems like any number of things could go wrong when trying to write to a redirected standard input stream.Touchline
True, but you'll have to ignore all of them. Maybe log them first.Quotha
P
5

Use Marshal.GetHRForException() to detect the error code for the IOException. Some sample code to help you fight the compiler:

using System;
using System.IO;
using System.Runtime.InteropServices;

class Program {
    static void Main(string[] args) {
        try {
            throw new IOException("test", unchecked((int)0x8007006d));
        }
        catch (IOException ex) {
            if (Marshal.GetHRForException(ex) != unchecked((int)0x8007006d)) throw;
        }
    }
}
Pyrogenic answered 22/7, 2014 at 0:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.