FormatException 類別

定義

引數格式無效或複合格式字串格式不正確時所擲回的例外狀況。The exception that is thrown when the format of an argument is invalid, or when a composite format string is not well formed.

public ref class FormatException : SystemException
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class FormatException : SystemException
type FormatException = class
    inherit SystemException
Public Class FormatException
Inherits SystemException
繼承
FormatException
衍生
屬性

備註

可能會因為下列其中一個原因而擲回例外狀況:FormatExceptionA FormatException exception can be thrown for one of the following reasons:

  • 在呼叫將字串轉換成其他資料類型的方法時, 字串不符合所需的模式。In a call to a method that converts a string to some other data type, the string doesn't conform to the required pattern. 這通常發生于Convert呼叫類別的某些方法Parse , 以及某些類型ParseExact的和方法時。This typically occurs when calling some methods of the Convert class and the Parse and ParseExact methods of some types.

    在大部分情況下, 特別是當您要轉換的字串是由使用者輸入或從檔案讀取時, 如果轉換失敗, try/catch您應該使用區塊FormatException並處理例外狀況。In most cases, particularly if the string that you're converting is input by a user or read from a file, you should use a try/catch block and handle the FormatException exception if the conversion is unsuccessful. 您也可以使用TryParseTryParseExact方法的呼叫來取代轉換方法的呼叫 (如果有的話)。You can also replace the call to the conversion method with a call to a TryParse or TryParseExact method, if one exists. 不過, 當FormatException您嘗試剖析預先定義或硬式編碼的字串時, 所擲回的例外狀況表示程式發生錯誤。However, a FormatException exception that is thrown when you're trying to parse a predefined or hard-coded string indicates a program error. 在此情況下, 您應該更正錯誤, 而不是處理例外狀況。In this case, you should correct the error rather than handle the exception.

    將字串轉換成System命名空間中的下列類型可能會FormatException擲回例外狀況:The conversion of a string to the following types in the System namespace can throw a FormatException exception:

    • Boolean.Boolean. Boolean.Parse(String)Convert.ToBoolean(String)方法需要將字串轉換為 "true"、"true"、"false" 或 "false"。The Boolean.Parse(String) and Convert.ToBoolean(String) methods require the string to be converted to be "True", "true", "False", or "false". 任何其他值FormatException都會擲回例外狀況。Any other value throws a FormatException exception.

    • DateTimeDateTimeOffsetDateTime and DateTimeOffset. 所有日期和時間資料都是根據特定文化特性的格式設定慣例來解讀: 目前的執行緒文化特性 (或在某些情況下, 也就是目前的應用程式網域文化特性)、不因文化特性而異, 或指定的文化特性。All date and time data is interpreted based on the formatting conventions of a particular culture: either the current thread culture (or, in some cases, the current application domain culture), the invariant culture, or a specified culture. DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles)您呼叫和DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles)方法時, 日期和時間資料也必須完全符合一或多個標準格式字串自訂格式字串所指定的模式, 這些是在方法呼叫。When you call the DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles) and DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) methods, date and time data must also conform exactly to a pattern specified by one or more standard format strings or custom format strings that are provided as arguments in the method call. 如果它不符合預期的特定文化特性模式, FormatException則會擲回例外狀況。If it doesn't conform to an expected culture-specific pattern, a FormatException exception is thrown. 這表示在某個系統上以特定文化特性格式儲存的日期和時間資料, 可能無法在另一個系統上成功剖析。This means that date and time data saved in a culture-specific format on one system might not parse successfully on another system.

      如需剖析日期和時間的詳細資訊, 請參閱剖析日期和時間字串和擲回例外狀況之方法的檔。For more information about parsing dates and times, see Parsing Date and Time Strings and the documentation for the method that threw the exception.

    • Guid.GUIDs. GUID 的字串表示必須包含32個十六進位數位 (0-F), 而且必須是Guid.ToString方法輸出的五個格式之一。The string representation of a GUID must consist of 32 hexadecimal digits (0-F), and must be in one of the five formats output by the Guid.ToString method. 如需詳細資訊,請參閱 Guid.Parse 方法。For more information, see the Guid.Parse method.

    • 數數值型別, 包括所有帶正負號的整數、不帶正負號的整數和浮點類型。Numeric types, including all signed integers, unsigned integers, and floating-point types. 要剖析的字串必須由拉丁數位0-9 組成。The string to be parsed must consist of the Latin digits 0-9. 可能也會允許正負號、小數分隔符號、群組分隔符號和貨幣符號。A positive or negative sign, decimal separator, group separators, and currency symbol may also be permitted. 嘗試剖析包含任何其他字元的字串時, 一律FormatException會擲回例外狀況。Trying to parse a string that contains any other character always throws a FormatException exception.

      所有數值字串都會根據特定文化特性的格式設定慣例來加以解讀: 目前的執行緒文化特性 (或在某些情況下, 也就是目前的應用程式網域文化特性)、不因文化特性而異, 或指定的文化特性。All numeric strings are interpreted based on the formatting conventions of a particular culture: either the current thread culture (or, in some cases, the current application domain culture), the invariant culture, or a specified culture. 如此一來, 使用另一個文化特性的慣例進行剖析的數值字串, 可能會在使用其他文化特性的慣例時失敗。As a result, a numeric string that is parsed by using the conventions of one culture might fail when using the conventions of another.

      如需剖析數值字串的詳細資訊, 請參閱剖析數值字串和擲回例外狀況之特定方法的檔。For more information about parsing numeric strings, see Parsing Numeric Strings and the documentation for the specific method that threw the exception.

    • 時間間隔。Time intervals. 要剖析的字串必須是固定文化特性格式或由目前線程文化特性所定義的區分文化特性格式 (或在某些情況下, 也就是目前的應用程式域文化特性)、不因文化特性而異, 或指定的文化特性。The string to be parsed must be either in fixed culture-insensitive format or in a culture-sensitive format defined by the current thread culture (or, in some cases, the current application domain culture), the invariant culture, or a specified culture. 如果字串不是適當的格式, 或如果沒有時間間隔的日、小時和分鐘元件, 則剖析方法FormatException會擲回例外狀況 (exception)。If the string isn't in an appropriate format, or if, at the minimum, the days, hours, and minutes components of the time interval aren't present, the parsing method throws a FormatException exception. 如需詳細資訊, 請參閱擲回TimeSpan例外狀況之剖析方法的檔。For more information, see the documentation for the TimeSpan parsing method that threw the exception.

  • 型別會執行IFormattable介面, 它支援定義物件如何轉換成字串標記法的格式字串, 並使用不正確格式字串。A type implements the IFormattable interface, which supports format strings that define how an object is converted to its string representation, and an invalid format string is used. 這在格式化作業中是最常見的。This is most common in a formatting operation. 在下列範例中, 複合格式字串中會使用 "Q" 標準格式字串來格式化數位。In the following example, the "Q" standard format string is used in a composite format string to format a number. 不過, "Q" 不是有效的標準格式字串However, "Q" is not a valid standard format string.

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

    這個例外狀況是來自程式碼錯誤所導致。This exception results from a coding error. 若要更正錯誤, 請移除格式字串, 或替換成有效的字串。To correct the error, either remove the format string or substitute a valid one. 下列範例會以 "C" (貨幣) 格式字串取代不正確格式字串, 以更正錯誤。The following example corrects the error by replacing the invalid format string with the "C" (currency) format string.

    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.
    
    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.
    

    剖析方法 ( DateTime.ParseExact例如和Guid.ParseExact) 可能也會擲回例外狀況,要求字串必須經過剖析,才能完全符合格式字串所指定的模式。FormatExceptionA FormatException exception can also be thrown by parsing methods, such as DateTime.ParseExact and Guid.ParseExact, that require the string to be parsed to conform exactly to the pattern specified by a format string. 在下列範例中, GUID 的字串標記法應符合 "G" 標準格式字串所指定的模式。In the following example, the string representation of a GUID is expected to conform to the pattern specified by the "G" standard format string. 不過, 結構GuidIFormattable實作為不支援 "G" 格式字串。However, the Guid structure's implementation of IFormattable does not support the "G" format string.

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

    此例外狀況也會因編碼錯誤而產生。This exception also results from a coding error. 若要修正此問題, 請呼叫不需要精確格式的剖析方法 (例如DateTime.ParseGuid.Parse), 或替換成有效的格式字串。To correct it, call a parsing method that doesn't require a precise format, such as DateTime.Parse or Guid.Parse, or substitute a valid format string. 下列範例會藉由呼叫Guid.Parse方法來更正錯誤。The following example corrects the error by calling the Guid.Parse method.

    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
    
    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
    
  • 複合格式字串中的一或多個格式專案的索引, 大於物件清單或參數陣列中專案的索引。One or more of the indexes of the format items in a composite format string is greater than the indexes of the items in the object list or parameter array. 在下列範例中, 格式專案的最大索引為3。In the following example, the largest index of a format item in the format string is 3. 因為物件清單中專案的索引是以零為起始, 所以此格式字串需要物件清單有四個專案。Because the indexes of items in the object list are zero-based, this format string would require the object list to have four items. 相反地, 它只有三個dattemp、和scale, 因此程式碼會在運行FormatException時間產生例外狀況:。Instead, it has only three, dat, temp, and scale, so the code results in a FormatException exception at run time:.

    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()
    
    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例外狀況是由開發人員錯誤所導致。In this case, the FormatException exception is a result of developer error. 您應該確定物件清單中的每個try/catch專案都對應至格式專案的索引, 而不是在區塊中進行處理。It should be corrected rather than handled in a try/catch block by making sure that each item in the object list corresponds to the index of a format item. 若要更正此範例, 請將第二個格式專案的索引變更為dat參考該變數, 並將每個後續格式專案的索引遞減一。To correct this example, change the index of the second format item to refer to the dat variable, and decrement the index of each subsequent format item by one.

    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
    
    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
    
  • 複合格式字串的格式不正確。The composite format string isn't well-formed. 發生這種情況時FormatException , 例外狀況一律是開發人員錯誤的結果。When this happens, the FormatException exception is always a result of developer error. 應加以更正, 而不是在try/catch區塊中處理。It should be corrected rather than handled in a try/catch block.

    如下列範例所示, 嘗試在字串中包含常值大括弧, 將會擲回例外狀況。Trying to include literal braces in a string, as the following example does, will throw the exception.

    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)
    

    在複合格式字串中包含常值大括弧的建議技巧是將它們包含在物件清單中, 並使用格式專案將其插入至結果字串。The recommended technique for including literal braces in a composite format string is to include them in the object list and use format items to insert them into the result string. 例如, 您可以修改先前的複合格式字串, 如下所示。For example, you can modify the previous composite format string as shown here.

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

    如果您的格式字串包含拼寫錯誤, 也會擲回例外狀況。The exception is also thrown if your format string contains a typo. 下列String.Format方法呼叫會省略右大括弧, 並將左大括弧與右括弧配對。The following call to the String.Format method omits a closing brace and pairs an opening brace with a closing bracket.

    int n1 = 10;
    int 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)
    

    若要更正錯誤, 請確定所有開頭和結尾大括弧都對應。To correct the error, ensure that all opening and closing braces correspond.

    String result = String.Format("{0} + {1} = {2}", 
                                  n1, n2, n1 + n2);
    
    Dim result As String = String.Format("{0} + {1} = {2}", 
                                         n1, n2, n1 + n2)
    
  • 您已在複合格式方法中, 將物件清單提供為強型別參數陣列, 而FormatException例外狀況表示一或多個格式專案的索引超過物件清單中的引數數目。You've supplied the object list in a composite formatting method as a strongly typed parameter array, and the FormatException exception indicates that the index of one or more format items exceeds the number of arguments in the object list. 發生這種情況是因為陣列類型之間沒有明確的轉換, 所以編譯器會將陣列視為單一引數, 而不是參數陣列。This occurs because no explicit conversion between array types exists, so instead the compiler treats the array as a single argument rather than as a parameter array. 例如, 下列Console.WriteLine(String, Object[])方法FormatException的呼叫會擲回例外狀況, 雖然格式專案的最高索引是 3, 且類型Int32的參數陣列具有四個元素。For example, the following call to the Console.WriteLine(String, Object[]) method throws a FormatException exception, although the highest index of the format items is 3, and the parameter array of type Int32 has four elements.

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

    您不需要處理這個例外狀況, 而是要排除其原因。Instead of handling this exception, you should eliminate its cause. 因為 Visual Basic 也C#不能將整數陣列轉換成物件陣列, 所以您必須先執行轉換, 然後再呼叫複合格式方法。Because neither Visual Basic nor C# can convert an integer array to an object array, you have to perform the conversion yourself before calling the composite formatting method. 下列範例提供一個實作為。The following example provides one implementation.

    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
    
    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 uses the HRESULT COR_E_FORMAT, which has the value 0x80131537.

類別衍生自Exception , 而且不會新增任何唯一成員。 FormatExceptionThe FormatException class derives from Exception and adds no unique members. 如需執行個體的初始屬性值的清單FormatException,請參閱FormatException建構函式。For a list of initial property values for an instance of FormatException, see the FormatException constructors.

建構函式

FormatException()

初始化 FormatException 類別的新執行個體。Initializes a new instance of the FormatException class.

FormatException(SerializationInfo, StreamingContext)

使用序列化資料,初始化 FormatException 類別的新執行個體。Initializes a new instance of the FormatException class with serialized data.

FormatException(String)

使用指定的錯誤訊息,初始化 FormatException 類別的新執行個體。Initializes a new instance of the FormatException class with a specified error message.

FormatException(String, Exception)

使用指定的錯誤訊息以及造成此例外狀況的內部例外狀況的參考,初始化 FormatException 類別的新執行個體。Initializes a new instance of the FormatException class with a specified error message and a reference to the inner exception that is the cause of this exception.

屬性

Data

取得提供例外狀況之其他使用者定義相關資訊的索引鍵/值組集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(繼承來源 Exception)
HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。Gets or sets a link to the help file associated with this exception.

(繼承來源 Exception)
HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。Gets the Exception instance that caused the current exception.

(繼承來源 Exception)
Message

取得描述目前例外狀況的訊息。Gets a message that describes the current exception.

(繼承來源 Exception)
Source

取得或設定造成錯誤的應用程式或物件的名稱。Gets or sets the name of the application or the object that causes the error.

(繼承來源 Exception)
StackTrace

取得呼叫堆疊上即時運算框架的字串表示。Gets a string representation of the immediate frames on the call stack.

(繼承來源 Exception)
TargetSite

取得擲回目前例外狀況的方法。Gets the method that throws the current exception.

(繼承來源 Exception)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 ExceptionWhen overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

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

在衍生類別中覆寫時,使用例外狀況的資訊設定 SerializationInfoWhen overridden in a derived class, sets the SerializationInfo with information about the exception.

(繼承來源 Exception)
GetType()

取得目前執行個體的執行階段類型。Gets the runtime type of the current instance.

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

建立並傳回目前例外狀況的字串表示。Creates and returns a string representation of the current exception.

(繼承來源 Exception)

事件

SerializeObjectState

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(繼承來源 Exception)

適用於

另請參閱