FormatException クラス

定義

引数の形式が無効である場合、または複合書式指定文字列が整形式でない場合にスローされる例外。

public ref class FormatException : Exception
public ref class FormatException : SystemException
public class FormatException : Exception
public class FormatException : SystemException
[System.Serializable]
public class FormatException : SystemException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class FormatException : SystemException
type FormatException = class
    inherit Exception
type FormatException = class
    inherit SystemException
[<System.Serializable>]
type FormatException = class
    inherit SystemException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type FormatException = class
    inherit SystemException
Public Class FormatException
Inherits Exception
Public Class FormatException
Inherits SystemException
継承
FormatException
継承
FormatException
派生
属性

注釈

例外は FormatException 、次のいずれかの理由でスローされる可能性があります。

  • 文字列を他のデータ型に変換するメソッドの呼び出しでは、文字列は必要なパターンに準拠していません。 これは通常、クラスの一部のメソッドと、一部のConvert型のメソッドをParse``ParseExact呼び出すときに発生します。

    ほとんどの場合、特に変換する文字列がユーザーによる入力またはファイルからの読み取りである場合は、(try/withF#で) ブロックをtry/catch使用し、変換が失敗した場合は例外を処理FormatExceptionする必要があります。 変換メソッドの呼び出しが存在する場合は、変換メソッドの呼び出しを a TryParse または TryParseExact メソッドの呼び出しに置き換えることもできます。 ただし、 FormatException 定義済みの文字列またはハードコーディングされた文字列を解析しようとしたときにスローされる例外は、プログラム エラーを示します。 この場合は、例外を処理するのではなく、エラーを修正する必要があります。

    名前空間内 System で文字列を次の型に変換すると、例外が FormatException スローされる可能性があります。

    • Boolean. メソッドではBoolean.Parse(String)Convert.ToBoolean(String)、文字列を "True"、"true"、"False"、または "false" に変換する必要があります。 その他の値は例外を FormatException スローします。

    • DateTime および DateTimeOffset。 すべての日時データは、現在のカルチャ (または場合によっては、現在のアプリケーション ドメイン カルチャ)、不変カルチャ、または指定したカルチャのいずれかの、特定のカルチャの書式設定規則に基づいて解釈されます。 メソッドをDateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles)DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles)呼び出す場合、日付と時刻のデータは、メソッド呼び出しで引数として提供される 1 つ以上の 標準書式指定文字列または カスタム書式指定文字列で指定されたパターンに 正確 に準拠している必要もあります。 予期されるカルチャ固有のパターンに準拠していない場合は、 FormatException 例外がスローされます。 つまり、あるシステムでカルチャ固有の形式で保存された日付と時刻のデータが、別のシステムで正常に解析されない可能性があります。

      日付と時刻の解析の詳細については、 日付と時刻の文字列の解析と 、例外をスローしたメソッドのドキュメントを参照してください。

    • GUID。 GUID の文字列形式は、32 桁の 16 進数 (0 から F) で構成され、メソッドによって Guid.ToString 出力される 5 つの形式のいずれかである必要があります。 詳細については、Guid.Parse メソッドを参照してください。

    • すべての符号付き整数、符号なし整数、浮動小数点型を含む数値型。 解析される文字列は、ラテン数字 0 から 9 で構成されている必要があります。 正符号または負符号、小数点記号、グループ区切り記号、および通貨記号も使用できます。 他の文字を含む文字列を解析しようとすると、常に例外が FormatException スローされます。

      すべての数値文字列は、特定のカルチャ (現在のカルチャ、不変カルチャ、または指定したカルチャ) の書式設定規則に基づいて解釈されます。 その結果、あるカルチャの規則を使用して解析される数値文字列は、別のカルチャの規則を使用すると失敗する可能性があります。

      数値文字列の解析の詳細については、 数値文字列の解析 と、例外をスローした特定のメソッドのドキュメントを参照してください。

    • 時間間隔。 解析する文字列は、固定カルチャに依存しない形式か、現在のカルチャ、インバリアント カルチャ、または指定したカルチャによって定義されたカルチャに依存する形式である必要があります。 文字列が適切な形式でない場合、または少なくとも時間間隔の日、時間、分のコンポーネントが存在しない場合、解析メソッドは例外を FormatException スローします。 詳細については、例外をスローした解析方法の TimeSpan ドキュメントを参照してください。

  • 型は、オブジェクトを IFormattable 文字列表現に変換する方法を定義する書式指定文字列をサポートし、無効な書式指定文字列が使用されるインターフェイスを実装します。 これは、書式設定操作で最も一般的です。 次の例では、数値を書式設定するために複合書式指定文字列で "Q" 標準書式指定文字列を使用します。 ただし、"Q" は有効な 標準書式指定文字列ではありません。

    using System;
    
    public class Example
    {
       public static void Main()
       {
          decimal price = 169.32m;
          Console.WriteLine("The cost is {0:Q2}.", price);
       }
    }
    // The example displays the following output:
    //    Unhandled Exception: System.FormatException: Format specifier was invalid.
    //       at System.Number.FormatDecimal(Decimal value, String format, NumberFormatInfo info)
    //       at System.Decimal.ToString(String format, IFormatProvider provider)
    //       at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
    //       at System.IO.TextWriter.WriteLine(String format, Object arg0)
    //       at System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0)
    //       at Example.Main()
    
    let price = 169.32m
    printfn $"The cost is {price:Q2}."
    // The example displays the following output:
    //    Unhandled Exception: System.FormatException: Format specifier was invalid.
    //       at System.Number.NumberToString(ValueStringBuilder& sb, NumberBuffer& number, Char format, Int32 nMaxDigits, NumberFormatInfo info)
    //       at System.Number.TryFormatDecimal(Decimal value, ReadOnlySpan`1 format, NumberFormatInfo info, Span`1 destination, Int32& charsWritten)
    //       at System.Decimal.TryFormat(Span`1 destination, Int32& charsWritten, ReadOnlySpan`1 format, IFormatProvider provider)
    //       at System.Text.ValueStringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
    //       at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
    //       at Microsoft.FSharp.Core.PrintfImpl.InterpolandToString@917.Invoke(Object vobj)
    //       at Microsoft.FSharp.Core.PrintfImpl.PrintfEnv`3.RunSteps(Object[] args, Type[] argTys, Step[] steps)
    //       at Microsoft.FSharp.Core.PrintfModule.gprintf[a,TState,TResidue,TResult,TPrinter](FSharpFunc`2 envf, PrintfFormat`4 format)
    //       at <StartupCode$fs>.$Example.main@()
    
    Module Example
       Public Sub Main()
          Dim price As Decimal = 169.32d
          Console.WriteLine("The cost is {0:Q2}.", price)
       End Sub
    End Module
    ' The example displays the following output:
    '    Unhandled Exception: System.FormatException: Format specifier was invalid.
    '       at System.Number.FormatDecimal(Decimal value, String format, NumberFormatInfo info)
    '       at System.Decimal.ToString(String format, IFormatProvider provider)
    '       at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
    '       at System.IO.TextWriter.WriteLine(String format, Object arg0)
    '       at System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0)
    '       at Example.Main()
    

    この例外は、コーディング エラーの結果として発生します。 エラーを修正するには、書式指定文字列を削除するか、有効な文字列に置き換えます。 次の例では、無効な書式指定文字列を "C" (通貨) 書式指定文字列に置き換えることで、エラーを修正します。

    using System;
    
    public class Example
    {
       public static void Main()
       {
          decimal price = 169.32m;
          Console.WriteLine("The cost is {0:C2}.", price);
       }
    }
    // The example displays the following output:
    //    The cost is $169.32.
    
    let price = 169.32m
    printfn $"The cost is {price:C2}."
    // The example displays the following output:
    //    The cost is $169.32.
    
    Module Example
       Public Sub Main()
          Dim price As Decimal = 169.32d
          Console.WriteLine("The cost is {0:C2}.", price)
       End Sub
    End Module
    ' The example displays the following output:
    '   The cost is $169.32.
    

    FormatExceptionまた、書式指定文字列で指定されたパターンに正確に準拠するように文字列を解析する必要がある、などのDateTime.ParseExactGuid.ParseExactメソッドを解析することによって例外をスローすることもできます。 次の例では、GUID の文字列表現は、"G" 標準書式指定文字列で指定されたパターンに準拠することが想定されています。 ただし、構造体 Guid の実装 IFormattable では、"G" 書式指定文字列はサポートされていません。

    using System;
    
    public class Example
    {
       public static void Main()
       {
          string guidString = "ba748d5c-ae5f-4cca-84e5-1ac5291c38cb";
          Console.WriteLine(Guid.ParseExact(guidString, "G"));
       }
    }
    // The example displays the following output:
    //    Unhandled Exception: System.FormatException:
    //       Format String can be only "D", "d", "N", "n", "P", "p", "B", "b", "X" or "x".
    //       at System.Guid.ParseExact(String input, String format)
    //       at Example.Main()
    
    open System
    
    let guidString = "ba748d5c-ae5f-4cca-84e5-1ac5291c38cb"
    printfn $"""{Guid.ParseExact(guidString, "G")}"""
    // The example displays the following output:
    //    Unhandled Exception: System.FormatException:
    //       Format String can be only "D", "d", "N", "n", "P", "p", "B", "b", "X" or "x".
    //       at System.Guid.ParseExact(String input, String format)
    //       at <StartupCode$fs>.$Example.main@()
    
    Module Example
       Public Sub Main()
          Dim guidString As String = "ba748d5c-ae5f-4cca-84e5-1ac5291c38cb"
          Console.WriteLine(Guid.ParseExact(guidString, "G"))
       End Sub
    End Module
    ' The example displays the following output:
    '    Unhandled Exception: System.FormatException: 
    '       Format String can be only "D", "d", "N", "n", "P", "p", "B", "b", "X" or "x".
    '       at System.Guid.ParseExact(String input, String format)
    '       at Example.Main()
    

    この例外は、コーディング エラーの結果でもあります。 修正するには、正確な書式を必要としない解析メソッドを呼び出すか、DateTime.ParseGuid.Parse有効な書式指定文字列を置き換えます。 次の例では、メソッドを呼び出してエラーを Guid.Parse 修正します。

    using System;
    
    public class Example
    {
       public static void Main()
       {
          string guidString = "ba748d5c-ae5f-4cca-84e5-1ac5291c38cb";
          Console.WriteLine(Guid.Parse(guidString));
       }
    }
    // The example displays the following output:
    //    ba748d5c-ae5f-4cca-84e5-1ac5291c38cb
    
    open System
    
    let guidString = "ba748d5c-ae5f-4cca-84e5-1ac5291c38cb"
    printfn $"{Guid.Parse guidString}"
    // The example displays the following output:
    //    ba748d5c-ae5f-4cca-84e5-1ac5291c38cb
    
    Module Example
       Public Sub Main()
          Dim guidString As String = "ba748d5c-ae5f-4cca-84e5-1ac5291c38cb"
          Console.WriteLine(Guid.Parse(guidString))
       End Sub
    End Module
    ' The example displays the following output:
    '   ba748d5c-ae5f-4cca-84e5-1ac5291c38cb
    
  • 複合書式指定文字列内の書式項目の 1 つ以上のインデックスが、オブジェクト リストまたはパラメーター配列内の項目のインデックスよりも大きい。 次の例では、書式指定文字列内の書式項目の最大インデックスは 3 です。 オブジェクト リスト内の項目のインデックスは 0 から始まるため、この書式指定文字列ではオブジェクト リストに 4 つの項目が必要になります。 代わりに、コードの実行時に例外が発生FormatExceptionするため、scale``dat``temp3 つだけです。

    using System;
    
    public class Example
    {
       public enum TemperatureScale
       { Celsius, Fahrenheit, Kelvin }
    
       public static void Main()
       {
          String info = GetCurrentTemperature();
          Console.WriteLine(info);
       }
    
       private static String GetCurrentTemperature()
       {
          DateTime dat = DateTime.Now;
          Decimal temp = 20.6m;
          TemperatureScale scale = TemperatureScale.Celsius;
          String result;
    
          result = String.Format("At {0:t} on {1:D}, the temperature is {2:F1} {3:G}",
                                 dat, temp, scale);
          return result;
       }
    }
    // The example displays output like the following:
    //    Unhandled Exception: System.FormatException: Format specifier was invalid.
    //       at System.Number.FormatDecimal(Decimal value, String format, NumberFormatInfo info)
    //       at System.Decimal.ToString(String format, IFormatProvider provider)
    //       at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
    //       at System.String.Format(IFormatProvider provider, String format, Object[] args)
    //       at Example.Main()
    
    open System
    
    type TemperatureScale =
        | Celsius = 0
        | Fahrenheit = 1
        | Kelvin = 2
    
    let getCurrentTemperature () =
        let dat = DateTime.Now
        let temp = 20.6m
        let scale = TemperatureScale.Celsius
        String.Format("At {0:t} on {1:D}, the temperature is {2:F1} {3:G}", dat, temp, scale)
    
    getCurrentTemperature ()
    |> printfn "%s"
    
    // The example displays output like the following:
    //    Unhandled Exception: System.FormatException: Format specifier was invalid.
    //       at System.Number.NumberToString(ValueStringBuilder& sb, NumberBuffer& number, Char format, Int32 nMaxDigits, NumberFormatInfo info)   
    //       at System.Number.TryFormatDecimal(Decimal value, ReadOnlySpan`1 format, NumberFormatInfo info, Span`1 destination, Int32& charsWritten)
    //       at System.Decimal.TryFormat(Span`1 destination, Int32& charsWritten, ReadOnlySpan`1 format, IFormatProvider provider)       
    //       at System.Text.ValueStringBuilder.AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
    //       at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
    //       at System.String.Format(String format, Object arg0, Object arg1, Object arg2)
    //       at Example.getCurrentTemperature()
    //       at <StartupCode$fs>.$Example.main@()
    
    Module Example
       Public Enum TemperatureScale As Integer
          Celsius
          Fahrenheit
          Kelvin
       End Enum
    
       Public Sub Main()
          Dim info As String = GetCurrentTemperature()
          Console.WriteLine(info)
       End Sub
    
       Private Function GetCurrentTemperature() As String
          Dim dat As Date = Date.Now
          Dim temp As Decimal = 20.6d
          Dim scale As TemperatureScale = TemperatureScale.Celsius
          Dim result As String 
          
          result = String.Format("At {0:t} on {1:D}, the temperature is {2:F1} {3:G}",
                                 dat, temp, scale)    
          Return result
       End Function
    End Module
    ' The example displays output like the following:
    '    Unhandled Exception: System.FormatException: Format specifier was invalid.
    '       at System.Number.FormatDecimal(Decimal value, String format, NumberFormatInfo info)
    '       at System.Decimal.ToString(String format, IFormatProvider provider)
    '       at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
    '       at System.String.Format(IFormatProvider provider, String format, Object[] args)
    '       at Example.Main()
    

    この場合、 FormatException 例外は開発者エラーの結果です。 オブジェクト リスト内の各項目が書式項目の try/catch インデックスに対応していることを確認することで、ブロック内で処理するのではなく、修正する必要があります。 この例を修正するには、変数を参照するように 2 番目の書式指定項目のインデックスを dat 変更し、後続の各書式項目のインデックスを 1 つずつデクリメントします。

    using System;
    
    public class Example
    {
       public enum TemperatureScale
       { Celsius, Fahrenheit, Kelvin }
    
       public static void Main()
       {
          String info = GetCurrentTemperature();
          Console.WriteLine(info);
       }
    
       private static String GetCurrentTemperature()
       {
          DateTime dat = DateTime.Now;
          Decimal temp = 20.6m;
          TemperatureScale scale = TemperatureScale.Celsius;
          String result;
    
          result = String.Format("At {0:t} on {0:D}, the temperature is {1:F1} {2:G}",
                                 dat, temp, scale);
          return result;
       }
    }
    // The example displays output like the following:
    //    At 10:40 AM on Wednesday, June 04, 2014, the temperature is 20.6 Celsius
    
    open System
    
    type TemperatureScale =
        | Celsius = 0
        | Fahrenheit = 1
        | Kelvin = 2
    
    let getCurrentTemperature () =
        let dat = DateTime.Now
        let temp = 20.6m
        let scale = TemperatureScale.Celsius
        String.Format("At {0:t} on {0:D}, the temperature is {1:F1} {2:G}", dat, temp, scale)
    
    getCurrentTemperature ()
    |> printfn "%s"
    
    // The example displays output like the following:
    //    At 10:40 AM on Wednesday, June 04, 2014, the temperature is 20.6 Celsius
    
    Module Example
       Public Enum TemperatureScale As Integer
          Celsius
          Fahrenheit
          Kelvin
       End Enum
    
       Public Sub Main()
          Dim info As String = GetCurrentTemperature()
          Console.WriteLine(info)
       End Sub
    
       Private Function GetCurrentTemperature() As String
          Dim dat As Date = Date.Now
          Dim temp As Decimal = 20.6d
          Dim scale As TemperatureScale = TemperatureScale.Celsius
          Dim result As String 
          
          result = String.Format("At {0:t} on {0:D}, the temperature is {1:F1} {2:G}",
                                 dat, temp, scale)    
          Return result
       End Function
    End Module
    ' The example displays output like the following:
    '       At 10:40 AM on Wednesday, June 04, 2014, the temperature is 20.6 Celsius
    
  • 複合書式指定文字列が整形式ではありません。 この場合、 FormatException 例外は常に開発者エラーの結果になります。 ブロック内で処理するのではなく、修正する try/catch 必要があります。

    次の例のように、文字列にリテラル中かっこを含めようとすると、例外がスローされます。

    result = String.Format("The text has {0} '{' characters and {1} '}' characters.",
                           nOpen, nClose);
    
    let result = 
        String.Format("The text has {0} '{' characters and {1} '}' characters.", nOpen, nClose)
    
    result = String.Format("The text has {0} '{' characters and {1} '}' characters.",
                           nOpen, nClose)
    

    複合書式指定文字列にリテラル中かっこを含めるには、それらをオブジェクト リストに含め、書式項目を使用して結果文字列に挿入することをお勧めします。 たとえば、次に示すように、前の複合書式指定文字列を変更できます。

    string result;
    int nOpen = 1;
    int nClose = 2;
    result = String.Format("The text has {0} '{{' characters and {1} '}}' characters.",
                           nOpen, nClose);
    Console.WriteLine(result);
    
    let result =
        String.Format("The text has {0} '{{' characters and {1} '}}' characters.", nOpen, nClose)
    
    result = String.Format("The text has {0} '{{' characters and {1} '}}' characters.",
                           nOpen, nClose)
    

    書式指定文字列に入力ミスが含まれている場合も、例外がスローされます。 メソッドの次の String.Format 呼び出しでは、右中かっこが省略され、開始中かっこと右かっこがペアになります。

    int n1 = 10;
    int n2 = 20;
    String result = String.Format("{0 + {1] = {2}",
                                  n1, n2, n1 + n2);
    
    let n1 = 10
    let n2 = 20
    String result = String.Format("{0 + {1] = {2}",
                                n1, n2, n1 + n2)
    
    Dim n1 As Integer = 10
    Dim n2 As Integer = 20
    Dim result As String = String.Format("{0 + {1] = {2}", 
                                         n1, n2, n1 + n2)
    

    エラーを修正するには、すべての開始中かっこと終了中かっこが対応していることを確認します。

    String result = String.Format("{0} + {1} = {2}",
                                  n1, n2, n1 + n2);
    
    let result = String.Format("{0} + {1} = {2}", n1, n2, n1 + n2)
    
    Dim result As String = String.Format("{0} + {1} = {2}", 
                                         n1, n2, n1 + n2)
    
  • 複合書式指定メソッドでオブジェクト リストを厳密に型指定されたパラメーター配列として指定しました。例外は、 FormatException 1 つ以上の書式項目のインデックスがオブジェクト リスト内の引数の数を超えていることを示します。 これは、配列型間に明示的な変換が存在しないために発生します。代わりに、コンパイラは配列をパラメーター配列としてではなく単一の引数として扱います。 たとえば、次のメソッドの Console.WriteLine(String, Object[]) 呼び出しでは例外がスロー FormatException されますが、書式項目のインデックスの最大インデックスは 3 で、型 Int32 のパラメーター配列には 4 つの要素があります。

    using System;
    using System.Collections.Generic;
    
    public class Example
    {
       public static void Main()
       {
          Random rnd = new Random();
          int[]  numbers = new int[4];
          int total = 0;
          for (int ctr = 0; ctr <= 2; ctr++) {
             int number = rnd.Next(1001);
             numbers[ctr] = number;
             total += number;
          }
          numbers[3] = total;
          Console.WriteLine("{0} + {1} + {2} = {3}", numbers);
       }
    }
    // The example displays the following output:
    //    Unhandled Exception:
    //    System.FormatException:
    //       Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
    //       at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
    //       at System.IO.TextWriter.WriteLine(String format, Object arg0)
    //       at System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0)
    //       at Example.Main()
    
    open System
    
    let rnd = Random()
    let numbers = Array.zeroCreate<int> 4
    let mutable total = 0
    for i = 0 to 2 do
        let number = rnd.Next 1001
        numbers[i] <- number
        total <- total + number
    numbers[3] <- total
    Console.WriteLine("{0} + {1} + {2} = {3}", numbers)
    
    // The example displays the following output:
    //    Unhandled Exception:
    //    System.FormatException:
    //       Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
    //       at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
    //       at System.IO.TextWriter.WriteLine(String format, Object arg0)
    //       at System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0)
    //       at <StartupCode$fs>.$Example.main@()
    
    Imports System.Collections.Generic
    
    Module Example
       Public Sub Main()
          Dim rnd As New Random()
          Dim numbers(3) As Integer
          Dim total As Integer = 0
          For ctr = 0 To 2
             Dim number As Integer = rnd.Next(1001)
             numbers(ctr) = number
             total += number
          Next
          numbers(3) = total
          Console.WriteLine("{0} + {1} + {2} = {3}", numbers)   
       End Sub
    End Module
    ' The example displays the following output:
    '    Unhandled Exception: 
    '    System.FormatException: 
    '       Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
    '       at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
    '       at System.IO.TextWriter.WriteLine(String format, Object arg0)
    '       at System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0)
    '       at Example.Main()
    

    この例外を処理する代わりに、その原因を排除する必要があります。 Visual Basicも C# も整数配列をオブジェクト配列に変換できないため、複合書式指定メソッドを呼び出す前に、自分で変換を実行する必要があります。 次の例で 1 つの実装を示します。

    using System;
    using System.Collections.Generic;
    
    public class Example
    {
       public static void Main()
       {
          Random rnd = new Random();
          int[]  numbers = new int[4];
          int total = 0;
          for (int ctr = 0; ctr <= 2; ctr++) {
             int number = rnd.Next(1001);
             numbers[ctr] = number;
             total += number;
          }
          numbers[3] = total;
          object[] values = new object[numbers.Length];
          numbers.CopyTo(values, 0);
          Console.WriteLine("{0} + {1} + {2} = {3}", values);
       }
    }
    // The example displays output like the following:
    //        477 + 956 + 901 = 2334
    
    open System
    
    let rnd = Random()
    let numbers = Array.zeroCreate<int> 4
    let mutable total = 0
    for i = 0 to 2 do
        let number = rnd.Next 1001
        numbers[i] <- number
        total <- total + number
    numbers[3] <- total
    let values = Array.zeroCreate<obj> numbers.Length
    numbers.CopyTo(values, 0)
    Console.WriteLine("{0} + {1} + {2} = {3}", values)
    // The example displays output like the following:
    //        477 + 956 + 901 = 2334
    
    Imports System.Collections.Generic
    
    Module Example
       Public Sub Main()
          Dim rnd As New Random()
          Dim numbers(3) As Integer
          Dim total As Integer = 0
          For ctr = 0 To 2
             Dim number As Integer = rnd.Next(1001)
             numbers(ctr) = number
             total += number
          Next
          numbers(3) = total
          Dim values(numbers.Length - 1) As Object
          numbers.CopyTo(values, 0) 
          Console.WriteLine("{0} + {1} + {2} = {3}", values)   
       End Sub
    End Module
    ' The example displays output like the following:
    '       477 + 956 + 901 = 2334
    

FormatException は、0x80131537値を持つ HRESULT COR_E_FORMATを使用します。

このクラスは FormatException 、一意のメンバーから Exception 派生し、追加しません。 インスタンスの初期プロパティ値の一覧についてはFormatExceptionを参照してください、FormatExceptionコンス トラクター。

コンストラクター

FormatException()

FormatException クラスの新しいインスタンスを初期化します。

FormatException(SerializationInfo, StreamingContext)

シリアル化したデータを使用して、FormatException クラスの新しいインスタンスを初期化します。

FormatException(String)

指定したエラー メッセージを使用して、FormatException クラスの新しいインスタンスを初期化します。

FormatException(String, Exception)

指定したエラー メッセージおよびこの例外の原因となった内部例外への参照を使用して、FormatException クラスの新しいインスタンスを初期化します。

プロパティ

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)

適用対象

こちらもご覧ください