Discussion:
More granular error handling with exceptions
(too old to reply)
Anton Shepelev
2018-06-29 09:12:08 UTC
Permalink
Hello, all

Since the .NET standard libraries use Exceptions in-
stead of error codes, I find it hard to analyse spe-
cific error situations. For an example, when
File.Open() fails with an IOException, how can I
check whether it is because the file already exists
or for some other reason?
--
() ascii ribbon campaign - against html e-mail
/\ http://preview.tinyurl.com/qcy6mjc [archived]
😉 Good Guy 😉
2018-06-29 17:20:02 UTC
Permalink
Post by Anton Shepelev
Hello, all
Since the .NET standard libraries use Exceptions in-
stead of error codes, I find it hard to analyse spe-
cific error situations. For an example, when
File.Open() fails with an IOException, how can I
check whether it is because the file already exists
or for some other reason?
// Catch the IOException generated if the
// specified part of the file is not created.
catch(IOException e)
{
Console.WriteLine(
"{0}: The write operation could not " +
"be performed because the specified " +
"file is not created.",
e.GetType().Name);
}
/--- This email has been checked for viruses by
Windows Defender software.
//https://www.microsoft.com/en-gb/windows/comprehensive-security/
--
With over 950 million devices now running Windows 10, customer
satisfaction is higher than any previous version of windows.
Anton Shepelev
2018-06-30 12:50:41 UTC
Permalink
Post by Anton Shepelev
Since the .NET standard libraries use Exceptions
instead of error codes, I find it hard to anal-
yse specific error situations. For an example,
when File.Open() fails with an IOException, how
can I check whether it is because the file al-
ready exists or for some other reason?
You can write your own customized error message
[...]
I asked not about a custom error message but about
the handling of a specific error.
--
() ascii ribbon campaign -- against html e-mail
/\ http://preview.tinyurl.com/qcy6mjc [archived]
Arne Vajhøj
2018-06-29 23:58:56 UTC
Permalink
Post by Anton Shepelev
Since the .NET standard libraries use Exceptions in-
stead of error codes, I find it hard to analyse spe-
cific error situations. For an example, when
File.Open() fails with an IOException, how can I
check whether it is because the file already exists
or for some other reason?
For human handling then the exception is usually sufficient.

For programmatic handling you are dependent on that the
delveloper has subsclassed the exception properly so you can
catch different subtypes.

IOException is so so:

System.IO.IOException
        System.IO.DirectoryNotFoundException
        System.IO.DriveNotFoundException
        System.IO.EndOfStreamException
        System.IO.FileLoadException
        System.IO.FileNotFoundException
        System.IO.PathTooLongException
        System.IO.PipeException

Arne
Marcel Mueller
2018-06-30 05:49:13 UTC
Permalink
Post by Arne Vajhøj
For programmatic handling you are dependent on that the
delveloper has subsclassed the exception properly so you can
catch different subtypes.
System.IO.IOException
        System.IO.DirectoryNotFoundException
        System.IO.DriveNotFoundException
        System.IO.EndOfStreamException
        System.IO.FileLoadException
        System.IO.FileNotFoundException
        System.IO.PathTooLongException
        System.IO.PipeException
And if this is still not sufficient you may check the specific error
code. E.g.:
catch (IOException ex)
{ if (ex.HResult != 0x80070050) // ERROR_FILE_EXISTS
throw;
// your exception handler here
}


Marcel
Anton Shepelev
2018-06-30 12:52:40 UTC
Permalink
Post by Marcel Mueller
Post by Arne Vajhøj
For human handling then the exception is usually sufficient.
For programmatic handling you are dependent on that the
delveloper has subsclassed the exception properly so you can
catch different subtypes.
System.IO.IOException
System.IO.DirectoryNotFoundException
System.IO.DriveNotFoundException
System.IO.EndOfStreamException
System.IO.FileLoadException
System.IO.FileNotFoundException
System.IO.PathTooLongException
System.IO.PipeException
And if this is still not sufficient you may check the specific error
catch (IOException ex)
{ if (ex.HResult != 0x80070050) // ERROR_FILE_EXISTS
throw;
// your exception handler here
}
Thank you, Arne and Marcel.
--
() ascii ribbon campaign -- against html e-mail
/\ http://preview.tinyurl.com/qcy6mjc [archived]
Arne Vajhøj
2018-06-30 18:00:24 UTC
Permalink
Post by Marcel Mueller
Post by Arne Vajhøj
For programmatic handling you are dependent on that the
delveloper has subsclassed the exception properly so you can
catch different subtypes.
     System.IO.IOException
        System.IO.DirectoryNotFoundException
        System.IO.DriveNotFoundException
        System.IO.EndOfStreamException
        System.IO.FileLoadException
        System.IO.FileNotFoundException
        System.IO.PathTooLongException
        System.IO.PipeException
And if this is still not sufficient you may check the specific error
  catch (IOException ex)
  { if (ex.HResult != 0x80070050) // ERROR_FILE_EXISTS
      throw;
    // your exception handler here
  }
That is pretty cool.

I was not aware. Looks like the getter got public
in 4.5.

For something like IOException it should have the
error code from Win32 API.

Arne

Loading...