Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
This article provides supplementary remarks to the reference documentation for this API.
NotSupportedException indicates that no implementation exists for an invoked method or property.
NotSupportedException uses the HRESULT COR_E_NOTSUPPORTED
, which has the value 0x80131515.
For a list of initial property values for an instance of NotSupportedException, see the NotSupportedException constructors.
You might consider throwing a NotSupportedException exception in the following cases:
You're implementing a general-purpose interface, and number of the methods have no meaningful implementation. For example, if you are creating a date and time type that implements the IConvertible interface, you would throw a NotSupportedException exception for most of the conversions.
You've inherited from an abstract class that requires that you override a number of methods. However, you're only prepared to provide an implementation for a subset of these. For the methods that you decide not to implement, you can choose to throw a NotSupportedException.
You're defining a general-purpose type with a state that enables operations conditionally. For example, your type can be either read-only or read-write. In that case:
If the object is read-only, attempting to assign values to the properties of an instance or call methods that modify instance state should throw a NotSupportedException exception.
You should implement a property that returns a Boolean value that indicates whether particular functionality is available. For example, for a type that can be either read-only or read-write, you could implement a IsReadOnly
property that indicates whether the set of read-write methods are available or unavailable.
The NotSupportedException exception indicates that a method has no implementation and that you should not call it. You should not handle the exception. Instead, what you should do depends on the cause of the exception: whether an implementation is completely absent, or the member invocation is inconsistent with the purpose of an object (such as a call to the FileStream.Write method on a read-only FileStream object).
An implementation has not been provided because the operation cannot be performed in a meaningful way. This is a common exception when you are calling methods on an object that provides implementations for the methods of an abstract base class, or that implements a general-purpose interface, and the method has no meaningful implementation.
For example, the Convert class implements the IConvertible interface, which means that it must include a method to convert every primitive type to every other primitive type. Many of those conversions, however, are not possible. As a result, a call to the Convert.ToBoolean(DateTime) method, for instance, throws a NotSupportedException exception because there is no possible conversion between a DateTime and a Boolean value
To eliminate the exception, you should eliminate the method call.
The method call is not supported given the state of the object. You're attempting to invoke a member whose functionality is unavailable because of the object's state. You can eliminate the exception in one of three ways:
You know the state of the object in advance, but you've invoked an unsupported method or property. In this case, the member invocation is an error, and you can eliminate it.
You know the state of the object in advance (usually because your code has instantiated it), but the object is mis-configured. The following example illustrates this issue. It creates a read-only FileStream object and then attempts to write to it.
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
public class Example
{
public static async Task Main()
{
Encoding enc = Encoding.Unicode;
String value = "This is a string to persist.";
Byte[] bytes = enc.GetBytes(value);
FileStream fs = new FileStream(@".\TestFile.dat",
FileMode.Open,
FileAccess.Read);
Task t = fs.WriteAsync(enc.GetPreamble(), 0, enc.GetPreamble().Length);
Task t2 = t.ContinueWith((a) => fs.WriteAsync(bytes, 0, bytes.Length));
await t2;
fs.Close();
}
}
// The example displays the following output:
// Unhandled Exception: System.NotSupportedException: Stream does not support writing.
// at System.IO.Stream.BeginWriteInternal(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state
// , Boolean serializeAsynchronously)
// at System.IO.FileStream.BeginWrite(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback userCallback, Object sta
// teObject)
// at System.IO.Stream.<>c.<BeginEndWriteAsync>b__53_0(Stream stream, ReadWriteParameters args, AsyncCallback callback,
// Object state)
// at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMet
// hod, Func`3 endMethod)
// at System.IO.Stream.BeginEndWriteAsync(Byte[] buffer, Int32 offset, Int32 count)
// at System.IO.FileStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
// at System.IO.Stream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count)
// at Example.Main()
open System.IO
open System.Text
let main = task {
let enc = Encoding.Unicode
let value = "This is a string to persist."
let bytes = enc.GetBytes value
let fs = new FileStream(@".\TestFile.dat", FileMode.Open, FileAccess.Read)
let t = fs.WriteAsync(enc.GetPreamble(), 0, enc.GetPreamble().Length)
let t2 = t.ContinueWith(fun a -> fs.WriteAsync(bytes, 0, bytes.Length))
let! _ = t2
fs.Close()
}
main.Wait()
// The example displays the following output:
// Unhandled Exception: System.NotSupportedException: Stream does not support writing.
// at System.IO.Stream.BeginWriteInternal(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state
// , Boolean serializeAsynchronously)
// at System.IO.FileStream.BeginWrite(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback userCallback, Object sta
// teObject)
// at System.IO.Stream.<>c.<BeginEndWriteAsync>b__53_0(Stream stream, ReadWriteParameters args, AsyncCallback callback,
// Object state)
// at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMet
// hod, Func`3 endMethod)
// at System.IO.Stream.BeginEndWriteAsync(Byte[] buffer, Int32 offset, Int32 count)
// at System.IO.FileStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
// at System.IO.Stream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count)
// at <StartupCode:fs>.main()
Imports System.IO
Imports System.Text
Imports System.Threading.Tasks
Module Example
Public Sub Main()
Dim enc As Encoding = Encoding.Unicode
Dim value As String = "This is a string to persist."
Dim bytes() As Byte = enc.GetBytes(value)
Dim fs As New FileStream(".\TestFile.dat",
FileMode.Open,
FileAccess.Read)
Dim t As Task = fs.WriteAsync(enc.GetPreamble(), 0, enc.GetPreamble().Length)
Dim t2 As Task = t.ContinueWith(Sub(a) fs.WriteAsync(bytes, 0, bytes.Length))
t2.Wait()
fs.Close()
End Sub
End Module
' The example displays the following output:
' Unhandled Exception: System.NotSupportedException: Stream does not support writing.
' at System.IO.Stream.BeginWriteInternal(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state
' , Boolean serializeAsynchronously)
' at System.IO.FileStream.BeginWrite(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback userCallback, Object sta
' teObject)
' at System.IO.Stream.<>c.<BeginEndWriteAsync>b__53_0(Stream stream, ReadWriteParameters args, AsyncCallback callback,
' Object state)
' at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMet
' hod, Func`3 endMethod)
' at System.IO.Stream.BeginEndWriteAsync(Byte[] buffer, Int32 offset, Int32 count)
' at System.IO.FileStream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
' at System.IO.Stream.WriteAsync(Byte[] buffer, Int32 offset, Int32 count)
' at Example.Main()
Ycan eliminate the exception by ensuring that the instantiated object supports the functionality you intend. The following example addresses the problem of the read-only FileStream object by providing the correct arguments to the FileStream.FileStream(String, FileMode, FileAccess) constructor.
You don't know the state of the object in advance, and the object doesn't support a particular operation. In most cases, the object should include a property or method that indicates whether it supports a particular set of operations. You can eliminate the exception by checking the value of the object and invoking the member only if appropriate.
The following example defines a DetectEncoding
method that throws a NotSupportedException exception when it attempts to read from the beginning of a stream that does not support read access.
using System;
using System.IO;
using System.Threading.Tasks;
public class TestPropEx1
{
public static async Task Main()
{
String name = @".\TestFile.dat";
var fs = new FileStream(name,
FileMode.Create,
FileAccess.Write);
Console.WriteLine("Filename: {0}, Encoding: {1}",
name, await FileUtilities1.GetEncodingType(fs));
}
}
public class FileUtilities1
{
public enum EncodingType
{ None = 0, Unknown = -1, Utf8 = 1, Utf16 = 2, Utf32 = 3 }
public async static Task<EncodingType> GetEncodingType(FileStream fs)
{
Byte[] bytes = new Byte[4];
int bytesRead = await fs.ReadAsync(bytes, 0, 4);
if (bytesRead < 2)
return EncodingType.None;
if (bytesRead >= 3 & (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF))
return EncodingType.Utf8;
if (bytesRead == 4)
{
var value = BitConverter.ToUInt32(bytes, 0);
if (value == 0x0000FEFF | value == 0xFEFF0000)
return EncodingType.Utf32;
}
var value16 = BitConverter.ToUInt16(bytes, 0);
if (value16 == (ushort)0xFEFF | value16 == (ushort)0xFFFE)
return EncodingType.Utf16;
return EncodingType.Unknown;
}
}
// The example displays the following output:
// Unhandled Exception: System.NotSupportedException: Stream does not support reading.
// at System.IO.FileStream.BeginRead(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback callback, Object state)
// at System.IO.Stream.<>c.<BeginEndReadAsync>b__46_0(Stream stream, ReadWriteParameters args, AsyncCallback callback, Object state)
// at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance, TArgs](TInstance thisRef, TArgs args, Func`5 beginMethod, Func`3 endMethod)
// at System.IO.Stream.BeginEndReadAsync(Byte[] buffer, Int32 offset, Int32 count)
// at System.IO.FileStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
// at System.IO.Stream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count)
// at FileUtilities.GetEncodingType(FileStream fs) in C:\Work\docs\program.cs:line 26
// at Example.Main() in C:\Work\docs\program.cs:line 13
// at Example.<Main>()
open System
open System.IO
module FileUtilities =
type EncodingType =
| None = 0
| Unknown = -1
| Utf8 = 1
| Utf16 = 2
| Utf32 = 3
let getEncodingType (fs: FileStream) =
task {
let bytes = Array.zeroCreate<byte> 4
let! bytesRead = fs.ReadAsync(bytes, 0, 4)
if bytesRead < 2 then
return EncodingType.None
elif bytesRead >= 3 && bytes[0] = 0xEFuy && bytes[1] = 0xBBuy && bytes[2] = 0xBFuy then
return EncodingType.Utf8
else
let value = BitConverter.ToUInt32(bytes, 0)
if bytesRead = 4 && (value = 0x0000FEFFu || value = 0xFEFF0000u) then
return EncodingType.Utf32
else
let value16 = BitConverter.ToUInt16(bytes, 0)
if value16 = 0xFEFFus || value16 = 0xFFFEus then
return EncodingType.Utf16
else
return EncodingType.Unknown
}
let main _ =
task {
let name = @".\TestFile.dat"
let fs = new FileStream(name, FileMode.Create, FileAccess.Write)
let! et = FileUtilities.getEncodingType fs
printfn $"Filename: {name}, Encoding: {et}"
}
// The example displays the following output:
// Unhandled Exception: System.NotSupportedException: Stream does not support reading.
// at System.IO.FileStream.BeginRead(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback callback, Object state)
// at System.IO.Stream.<>c.<BeginEndReadAsync>b__46_0(Stream stream, ReadWriteParameters args, AsyncCallback callback, Object state)
// at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance, TArgs](TInstance thisRef, TArgs args, Func`5 beginMethod, Func`3 endMethod)
// at System.IO.Stream.BeginEndReadAsync(Byte[] buffer, Int32 offset, Int32 count)
// at System.IO.FileStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
// at System.IO.Stream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count)
// at FileUtilities.GetEncodingType(FileStream fs)
// at Example.Main()
// at Example.<Main>()
Imports System.IO
Imports System.Threading.Tasks
Module Example2
Public Sub Main()
Dim name As String = ".\TestFile.dat"
Dim fs As New FileStream(name,
FileMode.Create,
FileAccess.Write)
Console.WriteLine("Filename: {0}, Encoding: {1}",
name, FileUtilities2.GetEncodingType(fs))
End Sub
End Module
Public Class FileUtilities2
Public Enum EncodingType As Integer
None = 0
Unknown = -1
Utf8 = 1
Utf16 = 2
Utf32 = 3
End Enum
Public Shared Function GetEncodingType(fs As FileStream) As EncodingType
Dim bytes(3) As Byte
Dim t As Task(Of Integer) = fs.ReadAsync(bytes, 0, 4)
t.Wait()
Dim bytesRead As Integer = t.Result
If bytesRead < 2 Then Return EncodingType.None
If bytesRead >= 3 And (bytes(0) = &HEF AndAlso bytes(1) = &HBB AndAlso bytes(2) = &HBF) Then
Return EncodingType.Utf8
End If
If bytesRead = 4 Then
Dim value As UInteger = BitConverter.ToUInt32(bytes, 0)
If value = &HFEFF Or value = &HFEFF0000 Then
Return EncodingType.Utf32
End If
End If
Dim value16 As UInt16 = BitConverter.ToUInt16(bytes, 0)
If value16 = &HFEFF Or value16 = &HFFFE Then
Return EncodingType.Utf16
End If
Return EncodingType.Unknown
End Function
End Class
' The example displays the following output:
' Unhandled Exception: System.NotSupportedException: Stream does not support reading.
' at System.IO.Stream.BeginReadInternal(Byte[] buffer, Int32 offset, Int32 count, AsyncCallback callback, Object state,
' Boolean serializeAsynchronously)
' at System.IO.FileStream.BeginRead(Byte[] array, Int32 offset, Int32 numBytes, AsyncCallback userCallback, Object stat
' eObject)
' at System.IO.Stream.<>c.<BeginEndReadAsync>b__43_0(Stream stream, ReadWriteParameters args, AsyncCallback callback, O
' bject state)
' at System.Threading.Tasks.TaskFactory`1.FromAsyncTrim[TInstance,TArgs](TInstance thisRef, TArgs args, Func`5 beginMet
' hod, Func`3 endMethod)
' at System.IO.Stream.BeginEndReadAsync(Byte[] buffer, Int32 offset, Int32 count)
' at System.IO.FileStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
' at System.IO.Stream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count)
' at FileUtilities2.GetEncodingType(FileStream fs)
' at Example.Main()
You can eliminate the exception by examining the value of the FileStream.CanRead property and exiting the method if the stream is read-only.
public static async Task<EncodingType> GetEncodingType(FileStream fs)
{
if (!fs.CanRead)
return EncodingType.Unknown;
Byte[] bytes = new Byte[4];
int bytesRead = await fs.ReadAsync(bytes, 0, 4);
if (bytesRead < 2)
return EncodingType.None;
if (bytesRead >= 3 & (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF))
return EncodingType.Utf8;
if (bytesRead == 4)
{
var value = BitConverter.ToUInt32(bytes, 0);
if (value == 0x0000FEFF | value == 0xFEFF0000)
return EncodingType.Utf32;
}
var value16 = BitConverter.ToUInt16(bytes, 0);
if (value16 == (ushort)0xFEFF | value16 == (ushort)0xFFFE)
return EncodingType.Utf16;
return EncodingType.Unknown;
}
}
// The example displays the following output:
// Filename: .\TestFile.dat, Encoding: Unknown
let getEncodingType (fs: FileStream) =
task {
if not fs.CanRead then
return EncodingType.Unknown
else
let bytes = Array.zeroCreate<byte> 4
let! bytesRead = fs.ReadAsync(bytes, 0, 4)
if bytesRead < 2 then
return EncodingType.None
elif bytesRead >= 3 && bytes[0] = 0xEFuy && bytes[1] = 0xBBuy && bytes[2] = 0xBFuy then
return EncodingType.Utf8
else
let value = BitConverter.ToUInt32(bytes, 0)
if bytesRead = 4 && (value = 0x0000FEFFu || value = 0xFEFF0000u) then
return EncodingType.Utf32
else
let value16 = BitConverter.ToUInt16(bytes, 0)
if value16 = 0xFEFFus || value16 = 0xFFFEus then
return EncodingType.Utf16
else
return EncodingType.Unknown
}
// The example displays the following output:
// Filename: .\TestFile.dat, Encoding: Unknown
Public Class FileUtilities3
Public Enum EncodingType As Integer
None = 0
Unknown = -1
Utf8 = 1
Utf16 = 2
Utf32 = 3
End Enum
Public Shared Function GetEncodingType(fs As FileStream) As EncodingType
If Not fs.CanRead Then
Return EncodingType.Unknown
End If
Dim bytes(3) As Byte
Dim t As Task(Of Integer) = fs.ReadAsync(bytes, 0, 4)
t.Wait()
Dim bytesRead As Integer = t.Result
If bytesRead < 2 Then Return EncodingType.None
If bytesRead >= 3 And (bytes(0) = &HEF AndAlso bytes(1) = &HBB AndAlso bytes(2) = &HBF) Then
Return EncodingType.Utf8
End If
If bytesRead = 4 Then
Dim value As UInteger = BitConverter.ToUInt32(bytes, 0)
If value = &HFEFF Or value = &HFEFF0000 Then
Return EncodingType.Utf32
End If
End If
Dim value16 As UInt16 = BitConverter.ToUInt16(bytes, 0)
If value16 = &HFEFF Or value16 = &HFFFE Then
Return EncodingType.Utf16
End If
Return EncodingType.Unknown
End Function
End Class
' The example displays the following output:
' Filename: .\TestFile.dat, Encoding: Unknown
The NotSupportedException exception is closely related to two other exception types;
This exception is thrown when a method could be implemented but is not, either because the member will be implemented in a later version, the member is not available on a particular platform, or the member belongs to an abstract class and a derived class must provide an implementation.
This exception is thrown in scenarios in which it is generally sometimes possible for the object to perform the requested operation, and the object state determines whether the operation can be performed.
When working with the .NET Compact Framework and using P/Invoke on a native function, this exception may be thrown if:
If a NotSupportedException exception is thrown, check:
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
Mar 17, 11 PM - Mar 21, 11 PM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Implement exception handling in C# console applications - Training
This module explores the use of exceptions and the exception handling process in C# console applications. Hands-on activities provide experience implementing exception handling patterns for various coding scenarios.