Alex Rivera | Logout

C# -Why does System.IO.File.GetLastAccessTime return an expected value when the file is not found?

Asked 2010-06-04T07:39:15.707
14

Please, explain your thoughts.

1.  DateTime dt = System.IO.File.GetLastAccessTime("C:\\There_is_no_such_file.txt");
2.  DateTime dt = System.IO.File.GetLastAccessTime("");
  1. If the file described in the path parameter does not exist, this method returns 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adjusted to local time.

  2. In the second situation argument exception is thrown.

Why in first case FileNotFoundException (or smth. simmilar) is not thrown?

Edit
Report

1 Answer

1

Well, I didn't write any of the System.IO library, so I can't claim to have the answer to what exceptions are thrown at what point. What qualifies as an exception will always be a decision for the developer to take.

I can take a stab at the reasoning behind this, though.

Having a file that doesn't exist may in a lot of cases be expected behavior. Having to hit the file system just to query whether a file exists, and then hit it again to get the access time for that file, might just have seemed like overhead, compared to simply hitting the file system once and verifying the result. If DateTime was nullable, this would probably have yielded null, just as one can imagine that IndexOf would have, instead of -1.

In the second case, however, passing an invalid path is evidence that somewhere in your code, something is maknig an expectation about something that cannot possibly work, and it might arguably make sense to bring this to the attention of the developer, by means of throwing an exception.

answered 2010-06-04T07:47:00.813

Your Answer