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可能會因為下列其中一個原因而擲回例外狀況:

  • 在將字串轉換成其他資料類型的方法呼叫中,字串不符合所需的模式。 呼叫類別 Parse 的某些方法和某些型別的 ConvertParseExact 方法時,通常會發生這種情況。

    在大部分情況下,特別是當您轉換的字串是由使用者輸入或從檔案讀取時,您應該在 F#) 區塊中使用 try/catch (try/with ,並在轉換失敗時處理 FormatException 例外狀況。 如果有呼叫 或 TryParseExact 方法,您也可以將對轉換方法的呼叫 TryParse 取代為 。 不過, FormatException 當您嘗試剖析預先定義或硬式編碼字串時所擲回的例外狀況表示程式錯誤。 在此情況下,您應該更正錯誤,而不是處理例外狀況。

    將字串轉換成命名空間中的 System 下列類型可能會擲回 FormatException 例外狀況:

    • Boolean. Boolean.Parse(String)Convert.ToBoolean(String) 方法需要將字串轉換成 「True」、「true」、「False」 或 「false」。 任何其他值都會擲回例外狀況 FormatException

    • DateTimeDateTimeOffset。 所有日期和時間資料都會根據特定文化特性的格式化慣例來解譯:目前的文化特性 (,或在某些情況下,目前的應用程式域文化特性) 、不因文化特性或指定的文化特性而異。 當您呼叫 DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles)DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) 方法時,日期和時間資料也必須 完全符合 由一或多個 標準格式字串自訂格式字串 所指定的模式,這些格式字串是在方法呼叫中當做引數提供。 如果不符合預期的特定文化特性模式, FormatException 則會擲回例外狀況。 這表示某個系統上以特定文化特性格式儲存的日期和時間資料可能不會在另一個系統上成功剖析。

      如需剖析日期和時間的詳細資訊,請參閱 剖析日期和時間字串 ,以及擲回例外狀況之方法的檔。

    • GUID。 GUID 的字串表示必須包含 32 個十六進位數位, (0-F) ,而且必須是 方法輸出 Guid.ToString 的五種格式之一。 如需詳細資訊,請參閱 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」 標準格式字串所指定的模式。 不過,結構的 GuidIFormattable 作不支援 「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
    
  • 複合格式字串中格式專案的一或多個索引大於物件清單或參數陣列中專案的索引。 在下列範例中,格式字串中格式專案的最大索引為 3。 由於物件清單中的專案索引是以零起始,因此此格式字串會要求物件清單有四個專案。 相反地,它只有三個 、 dat tempscale ,因此程式碼會在執行時間產生 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 處理,方法是確定物件清單中的每個專案都對應至格式專案的索引。 若要更正此範例,請變更第二個格式專案的索引以參考 dat 變數,並將每個後續格式專案的索引遞減一。

    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 例外狀況表示一或多個格式專案的索引超過物件清單中的引數數目。 這是因為陣列類型之間沒有明確轉換,因此編譯器會將陣列視為單一引數,而不是參數陣列。 例如,下列方法呼叫 Console.WriteLine(String, Object[])FormatException 擲回例外狀況,雖然格式專案的最高索引為 3,且 類型的 Int32 參數陣列有四個元素。

    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# 都無法將整數陣列轉換成物件陣列,所以您必須先自行執行轉換,再呼叫複合格式方法。 下列範例提供一個實作。

    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 會使用 HRESULT COR_E_FORMAT,其值為 0x80131537。

類別 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()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 Exception

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

在衍生類別中覆寫時,使用例外狀況的資訊設定 SerializationInfo

(繼承來源 Exception)
GetType()

取得目前執行個體的執行階段類型。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

建立並傳回目前例外狀況的字串表示。

(繼承來源 Exception)

事件

SerializeObjectState
已過時。

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。

(繼承來源 Exception)

適用於

另請參閱