共用方式為


ArrayTypeMismatchException 建構函式

定義

初始化 ArrayTypeMismatchException 類別的新執行個體。

多載

名稱 Description
ArrayTypeMismatchException()

初始化 ArrayTypeMismatchException 類別的新執行個體。

ArrayTypeMismatchException(String)

初始化類別的新實例 ArrayTypeMismatchException 並指定錯誤訊息。

ArrayTypeMismatchException(SerializationInfo, StreamingContext)
已淘汰.

使用串行化數據,初始化 ArrayTypeMismatchException 類別的新實例。

ArrayTypeMismatchException(String, Exception)

初始化類別的新實例 ArrayTypeMismatchException ,並附上指定的錯誤訊息及導致該異常的內部例外的參考。

ArrayTypeMismatchException()

來源:
ArrayTypeMismatchException.cs
來源:
ArrayTypeMismatchException.cs
來源:
ArrayTypeMismatchException.cs
來源:
ArrayTypeMismatchException.cs
來源:
ArrayTypeMismatchException.cs

初始化 ArrayTypeMismatchException 類別的新執行個體。

public:
 ArrayTypeMismatchException();
public ArrayTypeMismatchException();
Public Sub New ()

範例

以下範例展示了該 ArrayTypeMismatchException 類別的 ArrayTypeMismatchException() 建構子。 它包含一個函式,會接收兩個陣列作為參數,並檢查這兩個陣列是否屬於相同型態。 若陣列類型不同,會拋出一個新 ArrayTypeMismatchException 陣列,然後被呼叫方法捕捉。

using System;

public class ArrayTypeMismatchConst
{
    public void CopyArray(Array myArray, Array myArray1)
    {
        string typeArray1 = myArray.GetType().ToString();
        string typeArray2 = myArray1.GetType().ToString();
        // Check whether the two arrays are of same type or not.
        if (typeArray1 == typeArray2)
        {
            // Copy the values from one array to another.
            myArray.SetValue("Name: " + myArray1.GetValue(0), 0);
            myArray.SetValue("Name: " + myArray1.GetValue(1), 1);
        }
        else
        {
            // Throw an exception of type 'ArrayTypeMismatchException'.
            throw new ArrayTypeMismatchException();
        }
    }
    static void Main()
    {
        try
        {
            string[] myStringArray = new string[2];
            myStringArray.SetValue("Jones", 0);
            myStringArray.SetValue("John", 1);

            int[] myIntArray = new int[2];
            ArrayTypeMismatchConst myArrayType = new();
            myArrayType.CopyArray(myStringArray, myIntArray);
        }
        catch (ArrayTypeMismatchException e)
        {
            Console.WriteLine("The Exception is :" + e);
        }
    }
}
open System

let copyArray (myArray: Array) (myArray1: Array) =
    let typeArray1 = myArray.GetType() |> string
    let typeArray2 = myArray1.GetType() |> string
    // Check whether the two arrays are of same type or not.
    if typeArray1 = typeArray2 then
        // Copy the values from one array to another.
        myArray.SetValue($"Name: {myArray1.GetValue 0}", 0)
        myArray.SetValue($"Name: {myArray1.GetValue 1}", 1)
    else
        // Throw an exception of type 'ArrayTypeMismatchException'.
        raise (ArrayTypeMismatchException())

try
    let myStringArray = [| "Jones"; "John" |]

    let myIntArray = Array.zeroCreate<int> 2

    copyArray myStringArray myIntArray

with :? ArrayTypeMismatchException as e -> 
    printfn $"The Exception is: {e}"
Public Class ArrayTypeMisMatchConst
   Public Sub CopyArray(myArray As Array, myArray1 As Array)
      
      Dim typeArray1 As String = myArray.GetType().ToString()
      Dim typeArray2 As String = myArray1.GetType().ToString()
      ' Check whether the two arrays are of same type or not.
      If typeArray1 = typeArray2 Then
         ' Copy the values from one array to another.
         myArray.SetValue("Name: " + myArray1.GetValue(0), 0)
         myArray.SetValue("Name: " + myArray1.GetValue(1), 1)
      Else
         ' Throw an exception of type 'ArrayTypeMismatchException'.
         Throw New ArrayTypeMismatchException()
      End If
   End Sub

   Shared Sub Main()
      Try
         Dim myStringArray(1) As String
         myStringArray.SetValue("Jones", 0)
         myStringArray.SetValue("John", 1)
         Dim myIntArray(1) As Integer
         Dim myArrayType As New ArrayTypeMisMatchConst()
         myArrayType.CopyArray(myStringArray, myIntArray)
      Catch e As ArrayTypeMismatchException
         Console.WriteLine("The Exception is :" + e.ToString())
      End Try 
   End Sub
End Class

備註

此建構器會將新實例的屬性初始 Message 化為系統提供的訊息,描述錯誤,例如「來源陣列類型無法指派至目的陣列類型」。此訊息考量了當前系統文化。

下表顯示了 的 ArrayTypeMismatchException初始屬性值。

房產 價值
InnerException 一個空參考(Nothing Visual Basic 中的 Reference)。
Message 區域化錯誤訊息字串。

適用於

ArrayTypeMismatchException(String)

來源:
ArrayTypeMismatchException.cs
來源:
ArrayTypeMismatchException.cs
來源:
ArrayTypeMismatchException.cs
來源:
ArrayTypeMismatchException.cs
來源:
ArrayTypeMismatchException.cs

初始化類別的新實例 ArrayTypeMismatchException 並指定錯誤訊息。

public:
 ArrayTypeMismatchException(System::String ^ message);
public ArrayTypeMismatchException(string message);
public ArrayTypeMismatchException(string? message);
new ArrayTypeMismatchException : string -> ArrayTypeMismatchException
Public Sub New (message As String)

參數

message
String

描述錯誤的 String

範例

以下範例展示了該 ArrayTypeMismatchException 類別的 ArrayTypeMismatchException(String) 建構子。 它包含一個函式,會接收兩個陣列作為參數,並檢查這兩個陣列是否屬於相同型態。 若陣列類型不同,會拋出一個新 ArrayTypeMismatchException 陣列,然後被呼叫方法捕捉。

using System;

public class ArrayTypeMismatchConst2
{
    public void CopyArray(Array myArray, Array myArray1)
    {
        string typeArray1 = myArray.GetType().ToString();
        string typeArray2 = myArray1.GetType().ToString();
        // Check whether the two arrays are of same type or not.
        if (typeArray1 == typeArray2)
        {
            // Copies the values from one array to another.
            myArray.SetValue("Name: " + myArray1.GetValue(0), 0);
            myArray.SetValue("Name: " + myArray1.GetValue(1), 1);
        }
        else
        {
            // Throw an exception of type 'ArrayTypeMismatchException' with a message string as parameter.
            throw new ArrayTypeMismatchException("The Source and destination arrays are not of same type.");
        }
    }

    static void Main()
    {
        try
        {
            string[] myStringArray = new string[2];
            myStringArray.SetValue("Jones", 0);
            myStringArray.SetValue("John", 1);
            int[] myIntArray = new int[2];
            ArrayTypeMismatchConst2 myArrayType = new();
            myArrayType.CopyArray(myStringArray, myIntArray);
        }
        catch (ArrayTypeMismatchException e)
        {
            Console.WriteLine("The Exception Message is : " + e.Message);
        }
    }
}
open System

let copyArray (myArray: Array) (myArray1: Array) =
    let typeArray1 = myArray.GetType() |> string
    let typeArray2 = myArray1.GetType() |> string
    // Check whether the two arrays are of same type or not.
    if typeArray1 = typeArray2 then
        // Copy the values from one array to another.
        myArray.SetValue($"Name: {myArray1.GetValue 0}", 0)
        myArray.SetValue($"Name: {myArray1.GetValue 1}", 1)
    else
        // Throw an exception of type 'ArrayTypeMismatchException' with a message string as parameter.
        raise (ArrayTypeMismatchException "The Source and destination arrays are not of same type.")

try
    let myStringArray = [| "Jones"; "John" |]

    let myIntArray = Array.zeroCreate<int> 2

    copyArray myStringArray myIntArray

with :? ArrayTypeMismatchException as e -> 
    printfn $"The Exception is: {e}"

Public Class ArrayTypeMisMatchConst

   Public Sub CopyArray(myArray As Array, myArray1 As Array)

      Dim typeArray1 As String = myArray.GetType().ToString()
      Dim typeArray2 As String = myArray1.GetType().ToString()
      ' Check whether the two arrays are of same type or not.
      If typeArray1 = typeArray2 Then
         ' Copies the values from one array to another.
         myArray.SetValue("Name: " + myArray1.GetValue(0), 0)
         myArray.SetValue("Name: " + myArray1.GetValue(1), 1)
      Else
         ' Throw an exception of type 'ArrayTypeMismatchException' with a message string as parameter.
         Throw New ArrayTypeMismatchException("The Source and destination arrays are not of same type.")
      End If
   End Sub
   
   Shared Sub Main()
      Try
         Dim myStringArray(1) As String
         myStringArray.SetValue("Jones", 0)
         myStringArray.SetValue("John", 1)
         Dim myIntArray(1) As Integer
         Dim myArrayType As New ArrayTypeMisMatchConst()
         myArrayType.CopyArray(myStringArray, myIntArray)
      Catch e As ArrayTypeMismatchException
         Console.WriteLine(("The Exception Message is : " + e.Message))
      End Try
   End Sub
End Class

備註

參數的內容 message 是為了讓人類理解而設計的。 此建構器的呼叫者必須確保此字串已在目前系統文化中本地化。

下表顯示了 的 ArrayTypeMismatchException初始屬性值。

房產 價值
InnerException 一個空參考(Nothing Visual Basic 中的 Reference)。
Message 錯誤訊息字串。

適用於

ArrayTypeMismatchException(SerializationInfo, StreamingContext)

來源:
ArrayTypeMismatchException.cs
來源:
ArrayTypeMismatchException.cs
來源:
ArrayTypeMismatchException.cs
來源:
ArrayTypeMismatchException.cs
來源:
ArrayTypeMismatchException.cs

警告

This API supports obsolete formatter-based serialization. It should not be called or extended by application code.

使用串行化數據,初始化 ArrayTypeMismatchException 類別的新實例。

protected:
 ArrayTypeMismatchException(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected ArrayTypeMismatchException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
protected ArrayTypeMismatchException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new ArrayTypeMismatchException : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> ArrayTypeMismatchException
new ArrayTypeMismatchException : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> ArrayTypeMismatchException
Protected Sub New (info As SerializationInfo, context As StreamingContext)

參數

info
SerializationInfo

存放序列化物件資料的物件。

context
StreamingContext

關於來源或目的地的上下文資訊。

屬性

備註

此建構子在反序列化過程中被呼叫,以重建透過串流傳輸的例外物件。 欲了解更多資訊,請參閱 XML 與 SOAP 序列化

另請參閱

適用於

ArrayTypeMismatchException(String, Exception)

來源:
ArrayTypeMismatchException.cs
來源:
ArrayTypeMismatchException.cs
來源:
ArrayTypeMismatchException.cs
來源:
ArrayTypeMismatchException.cs
來源:
ArrayTypeMismatchException.cs

初始化類別的新實例 ArrayTypeMismatchException ,並附上指定的錯誤訊息及導致該異常的內部例外的參考。

public:
 ArrayTypeMismatchException(System::String ^ message, Exception ^ innerException);
public ArrayTypeMismatchException(string message, Exception innerException);
public ArrayTypeMismatchException(string? message, Exception? innerException);
new ArrayTypeMismatchException : string * Exception -> ArrayTypeMismatchException
Public Sub New (message As String, innerException As Exception)

參數

message
String

錯誤訊息解釋了例外原因。

innerException
Exception

該例外即為當前例外的原因。 若參數 innerException 非 null 參考,當前例外會在處理內部異常的區塊中被提出 catch

範例

以下程式碼範例展示了 ArrayTypeMismatchExceptionArrayTypeMismatchException 類別的建構子。 它包含一個函式,會接收兩個陣列作為參數,並檢查這兩個陣列是否屬於相同型態。 若陣列類型不同,會拋出一個新 ArrayTypeMismatchException 陣列,然後被呼叫方法捕捉。

using System;

public class ArrayTypeMismatchConst3
{
    public void CopyArray(Array myArray, Array myArray1)
    {
        try
        {
            // Copies the value of one array into another array.
            myArray.SetValue(myArray1.GetValue(0), 0);
            myArray.SetValue(myArray1.GetValue(1), 1);
        }
        catch (Exception e)
        {
            // Throw an exception of with a message and innerexception.
            throw new ArrayTypeMismatchException("The Source and destination arrays are of not same type.", e);
        }
    }
    static void Main()
    {
        try
        {
            string[] myStringArray = new string[2];
            myStringArray.SetValue("Jones", 0);
            myStringArray.SetValue("John", 1);
            int[] myIntArray = new int[2];
            ArrayTypeMismatchConst3 myArrayType = new();
            myArrayType.CopyArray(myStringArray, myIntArray);
        }
        catch (ArrayTypeMismatchException e)
        {
            Console.WriteLine("The Exception Message is : " + e.Message);
            Console.WriteLine("The Inner exception is :" + e.InnerException);
        }
    }
}
open System

let copyArray (myArray: Array) (myArray1: Array) =
    try
        // Copies the value of one array into another array.
        myArray.SetValue(myArray1.GetValue 0, 0)
        myArray.SetValue(myArray1.GetValue 1, 1)

    with e ->
        // Throw an exception of with a message and innerexception.
        raise (ArrayTypeMismatchException("The Source and destination arrays are of not same type.", e))

try
    let myStringArray = [| "Jones"; "John" |]
    let myIntArray = Array.zeroCreate<int> 2
    copyArray myStringArray myIntArray

with :? ArrayTypeMismatchException as e ->
    printfn $"The Exception Message is : {e.Message}"
    printfn $"The Inner exception is: {e.InnerException}"

Public Class ArrayTypeMisMatchConst

   Public Sub CopyArray(myArray As Array, myArray1 As Array)
      Try
         ' Copies the value of one array into another array.   
         myArray.SetValue(myArray1.GetValue(0), 0)
         myArray.SetValue(myArray1.GetValue(1), 1)
      Catch e As Exception
         ' Throw an exception of type 'ArrayTypeMismatchException' with a message and innerexception.
         Throw New ArrayTypeMismatchException("The Source and destination arrays are of not same type", e)
      End Try
   End Sub

   Shared Sub Main()
      Try
         Dim myStringArray(1) As String
         myStringArray.SetValue("Jones", 0)
         myStringArray.SetValue("John", 1)
         Dim myIntArray(1) As Integer
         Dim myArrayType As New ArrayTypeMisMatchConst()
         myArrayType.CopyArray(myStringArray, myIntArray)
      Catch e As ArrayTypeMismatchException
         Console.WriteLine("The Exception Message is : " + e.Message)
         Console.WriteLine("The Inner exception is :" + e.InnerException.ToString())
      End Try
   End Sub
End Class

備註

因先前例外直接拋出的例外,應包含對該屬性中 InnerException 先前例外的參考。 該InnerException屬性會回傳與傳入建構子相同的值,或若該InnerException屬性未提供內建器內部例外值,則回傳 Visual Basic 中的空參考Nothing

下表顯示了 的 ArrayTypeMismatchException初始屬性值。

房產 價值
InnerException 內部例外的參考。
Message 錯誤訊息字串。

另請參閱

適用於