FormatException クラス

定義

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

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

注釈

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

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

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

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

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

    • DateTimeDateTimeOffset. すべての日付と時刻データは、特定のカルチャの書式設定規則 (現在のカルチャ (場合によっては、現在のアプリケーション ドメイン カルチャ)、インバリアント カルチャ、または指定されたカルチャ) に基づいて解釈されます。 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 つの項目が必要になります。 代わりに、 dattemp、および scaleが 3 つしかないため、実行時に FormatException 例外が発生します。

    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, Exception)

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

FormatException(String)

指定したエラー メッセージを使用して、 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)

イベント

名前 説明
SerializeObjectState

例外に関するシリアル化されたデータを含む例外状態オブジェクトを作成するために例外がシリアル化されるときに発生します。

(継承元 Exception)

適用対象

こちらもご覧ください