Should my method throw its own exception, or let .NET throw if a file doesn't exist?

后端 未结 4 751
粉色の甜心
粉色の甜心 2020-12-14 05:17

Here is my code:

public void ReadSomeFile(string filePath)
{
    if (!File.Exists(filePath))
        throw new FileNotFoundException();

    var stream = new         


        
4条回答
  •  生来不讨喜
    2020-12-14 06:05

    Let the correct method try to open the file while you don't have any idea about full file name, somethings like special file names (eg. Device files and UNC paths):

    In some cases other file methods may be failed, but opening the file is successful.

    Some examples for special file names are:

    • CON
    • NUL
    • COM1, COM2, COM3, COM4
    • \\server\share\file_path
    • \\teela\admin$\system32 (to reach C:\WINNT\system32)
    • C:..\File.txt
    • \\.\COM1
    • %TEMP%
    • and more...

提交回复
热议问题