dotnet / dotnet/dotnet-api-docs

Documented and thrown exceptions for FileStream Length and Position does not align.

Open
#6,760 1 comment 0 reactions 0 assignees View on GitHub
area-System.IO Pri3 untriaged
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.