FormatException Sınıf

Tanım

Bağımsız değişkenin biçimi geçersiz olduğunda veya bileşik biçim dizesi iyi biçimlendirilmediğinde oluşturulan özel durum.

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
Devralma
FormatException
Devralma
FormatException
Türetilmiş
Öznitelikler

Açıklamalar

FormatException Aşağıdaki nedenlerden biri nedeniyle özel durum oluşturulabilir:

  • Dizeyi başka bir veri türüne dönüştüren bir yöntem çağrısında, dize gerekli desene uymaz. Bu genellikle sınıfın bazı yöntemlerini ve bazı türlerin Convert Parse ve ParseExact yöntemlerini çağırırken oluşur.

    Çoğu durumda, özellikle dönüştürdüğünüz dize bir kullanıcı tarafından girdiyse veya bir dosyadan okunuyorsa, dönüştürme başarısız olursa ( try/catch``try/with F#'ta) bloğunu kullanmanız ve özel durumu işlemeniz FormatException gerekir. Dönüştürme yöntemi çağrısı varsa, bir veya TryParseExact yöntemi çağrısıyla TryParse da değiştirebilirsiniz. Ancak, FormatException önceden tanımlanmış veya sabit kodlanmış bir dizeyi ayrıştırmaya çalışırken oluşan bir özel durum, program hatasını gösterir. Bu durumda, özel durumu işlemek yerine hatayı düzeltmeniz gerekir.

    Bir dizenin ad alanında System aşağıdaki türlere dönüştürülmesi özel durum FormatException oluşturabilir:

    • Boolean. Boolean.Parse(String) ve Convert.ToBoolean(String) yöntemleri, dizenin "True", "true", "False" veya "false" olarak dönüştürülmesini gerektirir. Başka bir değer özel durum FormatException oluşturur.

    • DateTime ve DateTimeOffset. Tüm tarih ve saat verileri, belirli bir kültürün biçimlendirme kurallarına göre yorumlanır: geçerli kültür (veya bazı durumlarda geçerli uygulama etki alanı kültürü), sabit kültür veya belirtilen kültür. ve DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles) yöntemlerini çağırdığınızda, tarih ve saat verilerinin DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles) de yöntem çağrısında bağımsız değişken olarak sağlanan bir veya daha fazla standart biçim dizesi veya özel biçim dizeleri tarafından belirtilen bir desene tam olarak uyması gerekir. Beklenen kültüre özgü bir desene uymuyorsa, bir FormatException özel durum oluşturulur. Bu, bir sistemde kültüre özgü biçimde kaydedilen tarih ve saat verilerinin başka bir sistemde başarıyla ayrıştırılmayabileceği anlamına gelir.

      Tarih ve saatleri ayrıştırma hakkında daha fazla bilgi için bkz. Tarih ve Saat Dizelerini Ayrıştırma ve özel durum oluşturan yöntemin belgeleri.

    • Guıd. GUID'nin dize gösterimi 32 onaltılık basamak (0-F) içermeli ve yöntemi tarafından çıkış olarak gösterilen beş biçimden Guid.ToString birinde olmalıdır. Daha fazla bilgi için yöntemine Guid.Parse bakın.

    • Tüm imzalı tamsayılar, işaretsiz tamsayılar ve kayan nokta türleri dahil olmak üzere sayısal türler. Ayrıştırılacak dize 0-9 Latin rakamlarından oluşmalıdır. Pozitif veya negatif işareti, ondalık ayırıcısı, grup ayırıcıları ve para birimi simgesine de izin verilebilir. Başka bir karakter içeren bir dizeyi ayrıştırmaya çalışmak her zaman bir FormatException özel durum oluşturur.

      Tüm sayısal dizeler, belirli bir kültürün biçimlendirme kurallarına göre yorumlanır: geçerli kültür, sabit kültür veya belirtilen bir kültür. Sonuç olarak, bir kültürün kuralları kullanılarak ayrıştırılan sayısal bir dize, başka bir kültürün kuralları kullanılırken başarısız olabilir.

      Sayısal dizeleri ayrıştırma hakkında daha fazla bilgi için bkz. Sayısal Dizeleri Ayrıştırma ve özel durum oluşturan belirli yöntemin belgeleri.

    • Zaman aralıkları. Ayrıştırılacak dize sabit kültüre duyarsız biçimde veya geçerli kültür, sabit kültür veya belirtilen bir kültür tarafından tanımlanan kültüre duyarlı bir biçimde olmalıdır. Dize uygun bir biçimde değilse veya en azından zaman aralığının gün, saat ve dakika bileşenleri yoksa, ayrıştırma yöntemi bir FormatException özel durum oluşturur. Daha fazla bilgi için TimeSpan , özel durumu oluşturan ayrıştırma yönteminin belgelerine bakın.

  • Tür, bir nesnenin dize gösterimine IFormattable nasıl dönüştürüldüğünü tanımlayan biçim dizelerini destekleyen arabirimini uygular ve geçersiz bir biçim dizesi kullanılır. Bu en yaygın kullanılan biçimlendirme işlemidir. Aşağıdaki örnekte, "Q" standart biçim dizesi bir sayıyı biçimlendirmek için bileşik biçim dizesinde kullanılır. Ancak, "Q" geçerli bir standart biçim dizesi değildir.

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

    Bu özel durum bir kodlama hatasından kaynaklanır. Hatayı düzeltmek için biçim dizesini kaldırın veya geçerli bir dizeyi kullanın. Aşağıdaki örnek, geçersiz biçim dizesini "C" (para birimi) biçim dizesiyle değiştirerek hatayı düzeltir.

    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.
    

    AyrıcaFormatException, dizenin bir biçim dizesi tarafından belirtilen desene tam olarak uyması için ayrıştırılması gereken ve Guid.ParseExactgibi DateTime.ParseExact ayrıştırma yöntemleri tarafından özel durum oluşturulabilir. Aşağıdaki örnekte, bir GUID'nin dize gösteriminin "G" standart biçim dizesi tarafından belirtilen desene uyması beklenir. Ancak yapısının Guid uygulaması IFormattable "G" biçim dizesini desteklemez.

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

    Bu özel durum bir kodlama hatasından da kaynaklanır. Düzeltmek için, veya Guid.Parsegibi DateTime.Parse kesin bir biçim gerektirmeyen bir ayrıştırma yöntemini çağırarak veya geçerli bir biçim dizesini kullanın. Aşağıdaki örnek, yöntemini çağırarak hatayı düzeltmektedir 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
    
  • Bileşik biçim dizesindeki biçim öğelerinin dizinlerinden biri veya daha fazlası nesne listesindeki veya parametre dizisindeki öğelerin dizinlerinden büyüktür. Aşağıdaki örnekte, biçim dizesindeki bir biçim öğesinin en büyük dizini 3'tür. Nesne listesindeki öğelerin dizinleri sıfır tabanlı olduğundan, bu biçim dizesi nesne listesinin dört öğeye sahip olmasını gerektirir. Bunun yerine yalnızca üç dat, tempve scaledeğeri vardır, bu nedenle kod çalışma zamanında bir FormatException özel durumla sonuçılır:

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

    Bu durumda, FormatException özel durum geliştirici hatasının bir sonucudur. Nesne listesindeki her öğenin bir try/catch biçim öğesinin dizinine karşılık olduğundan emin olunarak blokta işlenmek yerine düzeltilmelidir. Bu örneği düzeltmek için, ikinci biçim öğesinin dizinini değişkene başvuracak dat şekilde değiştirin ve izleyen her biçim öğesinin dizinini birer azaltın.

    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
    
  • Bileşik biçim dizesi iyi biçimlendirilmemiş. Bu durumda, FormatException özel durum her zaman geliştirici hatasının bir sonucudur. Bir blokta try/catch işlenmek yerine düzeltilmelidir.

    Aşağıdaki örnekte olduğu gibi bir dizeye değişmez ayraç eklemeye çalışmak özel durum oluşturur.

    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)
    

    Sabit ayraçları bileşik bir biçim dizesine eklemek için önerilen teknik, bunları nesne listesine eklemek ve sonuç dizesine eklemek için biçim öğelerini kullanmaktır. Örneğin, önceki bileşik biçim dizesini burada gösterildiği gibi değiştirebilirsiniz.

    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)
    

    Biçim dizenizde yazım hatası varsa özel durum da oluşturulur. Yöntemine yapılan String.Format aşağıdaki çağrı, bir kapanış ayracı atlar ve bir açılış ayracı kapanış ayracı ile eşler.

    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)
    

    Hatayı düzeltmek için tüm açma ve kapatma küme ayraçlarının karşılık gelen olduğundan emin olun.

    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)
    
  • Nesne listesini bileşik biçimlendirme yönteminde kesin olarak belirlenmiş bir parametre dizisi olarak sağladınız ve FormatException özel durum, bir veya daha fazla biçim öğesinin dizininin nesne listesindeki bağımsız değişkenlerin sayısını aştığını gösteriyor. Bunun nedeni dizi türleri arasında açık dönüştürme olmamasıdır, bu nedenle derleyici diziyi parametre dizisi yerine tek bir bağımsız değişken olarak ele alır. Örneğin, biçim öğelerinin en yüksek dizini 3 olmasına ve türdeki Int32 parametre dizisinin dört öğeye sahip olmasına rağmen, yöntemine yapılan aşağıdaki çağrı Console.WriteLine(String, Object[]) bir FormatException özel durum oluşturur.

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

    Bu özel durumu işlemek yerine nedenini ortadan kaldırmanız gerekir. Ne Visual Basic ne de C# bir tamsayı dizisini nesne dizisine dönüştüremediğinden, bileşik biçimlendirme yöntemini çağırmadan önce dönüştürmeyi kendiniz gerçekleştirmeniz gerekir. Aşağıdaki örnek bir uygulama sağlar.

    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 değeri 0x80131537 olan HRESULT COR_E_FORMAT kullanır.

FormatException sınıfı öğesinden Exception türetilir ve benzersiz üye eklemez. örneğinin ilk özellik değerlerinin FormatExceptionlistesi için oluşturuculara FormatException bakın.

Oluşturucular

FormatException()

FormatException sınıfının yeni bir örneğini başlatır.

FormatException(SerializationInfo, StreamingContext)

FormatException sınıfının yeni bir örneğini serileştirilmiş verilerle başlatır.

FormatException(String)

Belirtilen hata iletisiyle sınıfının yeni bir örneğini FormatException başlatır.

FormatException(String, Exception)

Belirtilen bir hata iletisi ve bu özel durumun nedeni olan iç özel duruma başvuru ile sınıfının yeni bir örneğini FormatException başlatır.

Özellikler

Data

Özel durum hakkında kullanıcı tanımlı ek bilgiler sağlayan bir anahtar/değer çifti koleksiyonu alır.

(Devralındığı yer: Exception)
HelpLink

Bu özel durumla ilişkili yardım dosyasının bağlantısını alır veya ayarlar.

(Devralındığı yer: Exception)
HResult

Belirli bir özel duruma atanan kodlanmış sayısal bir değer olan HRESULT değerini alır veya ayarlar.

(Devralındığı yer: Exception)
InnerException

Exception Geçerli özel duruma neden olan örneği alır.

(Devralındığı yer: Exception)
Message

Geçerli özel durumu açıklayan bir ileti alır.

(Devralındığı yer: Exception)
Source

Hataya neden olan uygulamanın veya nesnenin adını alır veya ayarlar.

(Devralındığı yer: Exception)
StackTrace

Çağrı yığınındaki anlık çerçevelerin dize gösterimini alır.

(Devralındığı yer: Exception)
TargetSite

Geçerli özel durumu oluşturan yöntemini alır.

(Devralındığı yer: Exception)

Yöntemler

Equals(Object)

Belirtilen nesnenin geçerli nesneye eşit olup olmadığını belirler.

(Devralındığı yer: Object)
GetBaseException()

Türetilmiş bir sınıfta geçersiz kılındığında, sonraki bir veya daha fazla özel durumun kök nedeni olan değerini döndürür Exception .

(Devralındığı yer: Exception)
GetHashCode()

Varsayılan karma işlevi işlevi görür.

(Devralındığı yer: Object)
GetObjectData(SerializationInfo, StreamingContext)

Türetilmiş bir sınıfta geçersiz kılındığında, özel durum hakkındaki bilgilerle öğesini ayarlar SerializationInfo .

(Devralındığı yer: Exception)
GetType()

Geçerli örneğin çalışma zamanı türünü alır.

(Devralındığı yer: Exception)
MemberwiseClone()

Geçerli Objectöğesinin sığ bir kopyasını oluşturur.

(Devralındığı yer: Object)
ToString()

Geçerli özel durumun dize gösterimini oluşturur ve döndürür.

(Devralındığı yer: Exception)

Ekinlikler

SerializeObjectState
Kullanımdan kalktı.

Bir özel durum, özel durum hakkında serileştirilmiş veriler içeren bir özel durum nesnesi oluşturmak üzere seri hale getirildiğinde gerçekleşir.

(Devralındığı yer: Exception)

Şunlara uygulanır

Ayrıca bkz.