Throw FileLoadException or just an IOException

I am writing an application and I want to download a file based on the path provided by the user. I check the file extension and when the extension does not match anything that I recognize, I want to throw an exception.

I could throw it away IOException. But I thought there might be a more detailed exception to this. So I looked at MSDN and found FileLoadException. The name suggests that my error will fit this category, but .... when I look at MSDN, it says: "Represents the error that occurs when the assembly file is found but cannot be loaded." and "An exception that is thrown when a managed assembly is found but cannot be loaded." This is absolutely not the case with my mistake.

So what is the question? Well, I wonder if the documentation really means that the exception is only for this purpose, or that they simply mean that they throw this exception in this particular case, but do not really indicate when others should throw it.

On a page IOExceptionon MSDN, he advises using FileLoadException, if necessary:

IOException - the base class for exceptions that occur when accessing information using streams, files and directories.

The base class library includes the following types, each of which is a derived class IOException:

  • DirectoryNotFoundException
  • EndOfStreamException
  • FileNotFoundException
  • FileLoadException
  • PathTooLongException

Use these types instead of an IOException if necessary.

: IOException FileLoadException ( ).

.

+3
5

IOException FileLoadException, "" ( " " ). FileLoadException Reflector ( mscorlib), , , msdn. , " ", - "

+3

, , , .

, IOException.

+3

FileLoadException, , , .

IOException , , .

, NotSupportedException, , , , .

+2

, : , .

, : , , , - .

, FileExtensionException, IOException. : , catch .

+2

, .

Why do you need to throw an exception and simply not inform the user that he has provided an extension that is not recognized by your application?

If you use OpenFileDialog, you can even use OpenFileDialog.Filterto allow the user to select only the file with the extension supported by your application.

0
source

Source: https://habr.com/ru/post/1723875/


All Articles