dotnet / dotnet/dotnet-api-docs
Documented and thrown exceptions for FileStream Length and Position does not align.
- Dominant language
- C#
- Stars
- 949
- Forks
- 1.7k
- Avg merge
- 3d 27m
- Merged PRs (30d)
- 49
Description
[FileStream.Length](https://docs.microsoft.com/en-us/dotnet/api/system.io.filestream.length) says it will throw an `IOException` when
> An I/O error, such as the file being closed, occurred.
As this example shows an `ObjectDisposedException` is thrown and it does not inherit from `IOException`.
```cs
var fs = System.IO.File.OpenRead("path_to_existing_file");
fs.Close();
_ = fs.Length; // throws ObjectDisposedException
```
In contrast [`Stream.Length`](https://docs.microsoft.com/en-us/dotnet/api/system.io.stream.length?view=net-5.0#exceptions), [`MemoryStream.Length`](https://docs.microsoft.com/en-us/dotnet/api/system.io.memorystream.length?view=net-5.0#exceptions) and [`BufferedStream.Length`](https://docs.microsoft.com/en-us/dotnet/api/system.io.bufferedstream.length?view=net-5.0#exceptions) all specify that an `ObjectDisposedException` will be thrown.
The same applies to the `Position` property.
Contributor guide
Assessment
This issue has not been assessed yet.