C# Reference ยท Exceptions and Diagnostics
NotImplementedException and NotSupportedException
Reference for NotImplementedException and NotSupportedException in C#: what each means, when to throw them, and how they differ from InvalidOperationException.
NotImplementedException and NotSupportedException both say "this member cannot do its job", but for very different reasons. NotImplementedException means the code has not been written yet: it is a placeholder that you intend to replace. NotSupportedException means the operation can never work for this object, by design.
IDE quick-fixes generate throw new NotImplementedException(); in stub methods. A release should contain none of them. NotSupportedException is a legitimate part of an API: a read-only stream throws it from Write, and a fixed-size or read-only collection throws it from Add.
void Export() => throw new NotImplementedException(); // todo void Write() => throw new NotSupportedException("Read-only."); // by design
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public class NotImplementedException : SystemException { public NotImplementedException(); public NotImplementedException(string? message); public NotImplementedException(string? message, Exception? inner); } public class NotSupportedException : SystemException { public NotSupportedException(); public NotSupportedException(string? message); public NotSupportedException(string? message, Exception? innerException); }
| Name | Description |
|---|---|
message |
Explanation shown in Message. If omitted, a generic default text is used. |
inner / innerException |
The exception that caused this one, kept in InnerException. |
Errors / undefined behavior: both are ordinary exceptions. They are thrown by your own code or by library members that do not support an operation. They are not thrown by the runtime for missing code.
Usage notes
NotImplementedExceptionis a to-do marker. Search for it before shipping. Analyzers and code reviews usually flag it. If a feature is intentionally absent, throwNotSupportedExceptioninstead.NotSupportedExceptionis part of the contract. Document which members throw it and offer a way to test first (CanWrite,CanSeek,IsReadOnly).- Prefer a design that avoids both. If an interface method makes no sense for your type, the interface is probably too big. Split it (see the Interface Segregation Principle in SOLID).
- Do not use either for wrong state. Calling a method at the wrong moment is
InvalidOperationException; a bad argument value is anArgumentException. - Derived behaviors can disagree. A base class may support an operation that a subclass refuses by throwing
NotSupportedException. That breaks substitutability, so use it sparingly. NotImplementedExceptioninside tests. A test that expects it is usually a leftover. Delete or finish it.- Both derive from
SystemExceptionand haveHResultvalues that identify them to interop callers. - Platform-specific APIs. Use
PlatformNotSupportedException(derived fromNotSupportedException) when something works on some operating systems only.