NotSupportedException クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
呼び出されたメソッドがサポートされていない場合、または呼び出された機能をサポートしないストリームに対して読み取り、シーク、または書き込みを行おうとした場合にスローされる例外。
public ref class NotSupportedException : Exception
public ref class NotSupportedException : SystemException
public class NotSupportedException : Exception
public class NotSupportedException : SystemException
[System.Serializable]
public class NotSupportedException : SystemException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class NotSupportedException : SystemException
type NotSupportedException = class
inherit Exception
type NotSupportedException = class
inherit SystemException
[<System.Serializable>]
type NotSupportedException = class
inherit SystemException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type NotSupportedException = class
inherit SystemException
Public Class NotSupportedException
Inherits Exception
Public Class NotSupportedException
Inherits SystemException
- 継承
- 継承
- 派生
- 属性
注釈
NotSupportedException は、呼び出されたメソッドまたはプロパティの実装が存在しないことを示します。
NotSupportedException は、0x80131515値を持つ HRESULT COR_E_NOTSUPPORTEDを使用します。
インスタンスの初期プロパティ値の一覧についてはNotSupportedExceptionを参照してください、NotSupportedExceptionコンス トラクター。
NotSupportedException 例外のスロー
次の場合は、例外を NotSupportedException スローすることを検討してください。
汎用インターフェイスを実装しており、メソッドの数には意味のある実装がありません。 たとえば、インターフェイスを実装 IConvertible する日付と時刻の型を作成する場合、ほとんどの変換で例外がスロー NotSupportedException されます。
多くのメソッドをオーバーライドする必要がある抽象クラスから継承しました。 ただし、これらのサブセットの実装を提供する準備は整っているだけです。 実装しないことを決定したメソッドの場合は、スロー NotSupportedExceptionを選択できます。
条件付きで操作を有効にする状態で汎用型を定義しています。 たとえば、型は読み取り専用または読み取り/書き込みのいずれかになります。 そのような場合は、次の処理を行います。
オブジェクトが読み取り専用の場合、インスタンスの状態を変更するインスタンスまたは呼び出しメソッドのプロパティに値を割り当てようとすると、例外が NotSupportedException スローされます。
特定の機能が使用可能かどうかを示す値を Boolean 返すプロパティを実装する必要があります。 たとえば、読み取り専用または読み取り/書き込み可能な型の場合、一連の読み取り/書き込みメソッドを使用できるか使用できないかを示すプロパティを実装
IsReadOnly
できます。
NotSupportedException 例外の処理
例外は NotSupportedException 、メソッドに実装がなく、呼び出してはならないことを示します。 例外を処理しないでください。 代わりに、実装が完全に存在しないか、メンバー呼び出しがオブジェクトの目的 (読み取り専用FileStreamオブジェクトでのメソッドの呼び出しなど) と矛盾しているかどうかは、例外のFileStream.Write原因によって異なります。
操作を意味のある方法で実行できないため、実装は提供されていません。
これは、抽象基本クラスのメソッドの実装を提供するオブジェクト、または汎用インターフェイスを実装するオブジェクトでメソッドを呼び出している場合に一般的な例外であり、メソッドに意味のある実装がない場合です。
たとえば、クラスは Convert インターフェイスを IConvertible 実装します。つまり、すべてのプリミティブ型を他のすべてのプリミティブ型に変換するメソッドを含める必要があります。 ただし、これらの変換の多くは不可能です。 その結果、メソッドをConvert.ToBoolean(DateTime)呼び出すと、たとえば、a と値の間DateTimeに変換できないので、例外がBooleanスローNotSupportedExceptionされます。
例外を排除するには、メソッド呼び出しを排除する必要があります。
オブジェクトの状態を考えると、メソッド呼び出しはサポートされていません。
オブジェクトの状態のために機能が利用できないメンバーを呼び出そうとしています。 例外は、次の 3 つの方法のいずれかで排除できます。
オブジェクトの状態は事前にわかっていますが、サポートされていないメソッドまたはプロパティを呼び出しました。 この場合、メンバー呼び出しはエラーであり、これを排除できます。
オブジェクトの状態は事前にわかっていますが (通常、コードでインスタンス化されているため)、オブジェクトの構成が正しくありません。 次の例は、この問題を示しています。 読み取り専用 FileStream オブジェクトを作成し、それに書き込もうとします。
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()
インスタンス化されたオブジェクトが目的の機能をサポートしていることを確認することで、例外を排除できます。 次の例では、コンストラクターに正しい引数を指定することで、読み取り専用 FileStream オブジェクトの問題に FileStream.FileStream(String, FileMode, FileAccess) 対処します。
オブジェクトの状態は事前に把握されておらず、オブジェクトは特定の操作をサポートしていません。 ほとんどの場合、オブジェクトには、特定の一連の操作をサポートするかどうかを示すプロパティまたはメソッドを含める必要があります。 オブジェクトの値を確認し、必要な場合にのみメンバーを呼び出すことで、例外を排除できます。
次の例では、読み取りアクセスを
DetectEncoding
サポートしていないストリームの先頭から読み取ろうとしたときに例外をスロー NotSupportedException するメソッドを定義します。using System; using System.IO; using System.Threading.Tasks; public class Example { 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 FileUtilities.GetEncodingType(fs)); } } public class FileUtilities { 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 Example 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, FileUtilities.GetEncodingType(fs)) End Sub End Module Public Class FileUtilities 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 = &h0000FEFF 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 FileUtilities.GetEncodingType(FileStream fs) ' at Example.Main()
プロパティの値 FileStream.CanRead を調べて、ストリームが読み取り専用の場合はメソッドを終了することで、例外を排除できます。
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 FileUtilities 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 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 = &h0000FEFF 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
関連する例外の種類
例外は NotSupportedException 、他の 2 つの例外の種類と密接に関連しています。
NotImplementedException.
この例外は、メソッドを実装することはできますが、新しいバージョンでメンバーが実装される、メンバーが特定のプラットフォームで使用できない、またはメンバーが抽象クラスに属しており、派生クラスが実装を提供する必要があるため、実装できない場合にスローされます。
InvalidOperationException
この例外は、通常、オブジェクトが要求された操作を実行できる場合があり、オブジェクトの状態によって操作を実行できるかどうかを決定するシナリオでスローされます。
.NET Compact Framework に関する注意事項
.NET Compact Framework を使用し、ネイティブ関数で P/Invoke を使用する場合、次の場合にこの例外がスローされる可能性があります。
マネージド コード内の宣言が正しくない。
.NET Compact Framework では、実行しようとしている操作はサポートされていません。
エクスポート時に DLL 名が変形処理されている。
NotSupportedException例外がスローされた場合は、次のことを確認します。
.NET Compact Framework P/Invoke の制限に違反している場合。
割り当て済みのメモリを必要とする引数。 存在する場合は、既存の変数への参照を渡す必要があります。
エクスポートされた関数の名前が正しいかどうか。 これは 、DumpBin.exeで確認できます。
渡そうとしている引数の数が多すぎないかどうか。
コンストラクター
NotSupportedException() |
NotSupportedException クラスの新しいインスタンスの Message プロパティを初期化し、その値としてエラーを説明するシステム提供のメッセージを指定します。 このメッセージには、システムの現在のカルチャが考慮されます。 |
NotSupportedException(SerializationInfo, StreamingContext) |
シリアル化したデータを使用して、NotSupportedException クラスの新しいインスタンスを初期化します。 |
NotSupportedException(String) |
指定したエラー メッセージを使用して、NotSupportedException クラスの新しいインスタンスを初期化します。 |
NotSupportedException(String, Exception) |
指定したエラー メッセージおよびこの例外の原因となった内部例外への参照を使用して、NotSupportedException クラスの新しいインスタンスを初期化します。 |
プロパティ
Data |
例外に関する追加のユーザー定義情報を提供する、キーと値のペアのコレクションを取得します。 (継承元 Exception) |
HelpLink |
この例外に関連付けられているヘルプ ファイルへのリンクを取得または設定します。 (継承元 Exception) |
HResult |
特定の例外に割り当てられているコード化数値である HRESULT を取得または設定します。 (継承元 Exception) |
InnerException |
現在の例外の原因となる Exception インスタンスを取得します。 (継承元 Exception) |
Message |
現在の例外を説明するメッセージを取得します。 (継承元 Exception) |
Source |
エラーの原因となるアプリケーションまたはオブジェクトの名前を取得または設定します。 (継承元 Exception) |
StackTrace |
呼び出し履歴で直前のフレームの文字列形式を取得します。 (継承元 Exception) |
TargetSite |
現在の例外がスローされたメソッドを取得します。 (継承元 Exception) |
メソッド
Equals(Object) |
指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
GetBaseException() |
派生クラスでオーバーライドされた場合、それ以後に発生する 1 つ以上の例外の根本原因である Exception を返します。 (継承元 Exception) |
GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
GetObjectData(SerializationInfo, StreamingContext) |
派生クラスでオーバーライドされた場合は、その例外に関する情報を使用して SerializationInfo を設定します。 (継承元 Exception) |
GetType() |
現在のインスタンスのランタイム型を取得します。 (継承元 Exception) |
MemberwiseClone() |
現在の Object の簡易コピーを作成します。 (継承元 Object) |
ToString() |
現在の例外の文字列形式を作成して返します。 (継承元 Exception) |
events
SerializeObjectState |
互換性のために残されています。
例外がシリアル化され、例外に関するシリアル化されたデータを含む例外状態オブジェクトが作成されたときに発生します。 (継承元 Exception) |