Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

ArrayTypeMismatchException Class

The exception that is thrown when an attempt is made to store an element of the wrong type within an array.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public class ArrayTypeMismatchException : SystemException
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public class ArrayTypeMismatchException extends SystemException
SerializableAttribute 
ComVisibleAttribute(true) 
public class ArrayTypeMismatchException extends SystemException
Not applicable.

ArrayTypeMismatchException is thrown when the system cannot convert the element to the type declared for the array. For example, an element of type String cannot be stored in an Int32 array because conversion between these types is not supported. It is generally unnecessary for applications to throw this exception.

The following Microsoft intermediate language (MSIL) instructions throw ArrayTypeMismatchException :

  • ldelem.<type>

  • ldelema

  • stelem.<type>

ArrayTypeMismatchException uses the HRESULT COR_E_ARRAYTYPEMISMATCH, which has the value 0x80131503.

For a list of initial property values for an instance of ArrayTypeMismatchException, see the ArrayTypeMismatchException constructors.

The following code example demonstrates scenarios where ArrayTypeMismatchException is thrown.

using System;

namespace ArrayTypeMismatch
{
    class Class1
    {
        static void Main(string[] args) 
        {
            string[] names = {"Dog", "Cat", "Fish"};
            Object[] objs  = (Object[]) names;

            try 
            {
                objs[2] = "Mouse";

                foreach (object animalName in objs) 
                {
                    System.Console.WriteLine(animalName);
                }
            }
            catch (System.ArrayTypeMismatchException) 
            {
                // Not reached; "Mouse" is of the correct type.
                System.Console.WriteLine("Exception Thrown.");
            }

            try 
            {
                Object obj = (Object) 13;
                objs[2] = obj;
            }
            catch (System.ArrayTypeMismatchException) 
            {
                // Always reached, 13 is not a string.
                System.Console.WriteLine(
                    "New element is not of the correct type.");
            }
    
            // Set objs to an array of objects instead of 
            // an array of strings.
            objs  = new Object[3];
            try 
            {
                objs[0] = (Object) "Turtle";
                objs[1] = (Object) 12;
                objs[2] = (Object) 2.341;

                foreach (object element in objs) 
                {
                    System.Console.WriteLine(element);
                }
            }
            catch (System.ArrayTypeMismatchException) 
            {
                // ArrayTypeMismatchException is not thrown this time.
                System.Console.WriteLine("Exception Thrown.");
            }
        }
    }
}

package ArrayTypeMismatch;

import System.*;
  
class Class1
{
    public static void main(String[] args)
    {
        String names[] = { "Dog", "Cat", "Fish" };
        Object objs[] = names;

        try {
            objs.set_Item(2, "Mouse");
            for (int iCtr = 0; iCtr < objs.length; iCtr++) {
                Console.WriteLine(objs[iCtr]);
            }
        }
        catch (System.ArrayTypeMismatchException exp) {
            // Not reached, "Mouse" is of the correct type.
            Console.WriteLine("Exception Thrown.");
        }
        try {
            objs[2] = (Int32)13;
        }
        catch (System.ArrayTypeMismatchException exp) {
            // Always reached, 13 is not a string.
            Console.WriteLine(("New element is not of the correct type."));
        }

        // Set objs to an array of objects instead of an array of strings.
        objs = new Object[3];
        try {
            objs.set_Item(0, "Turtle");
            objs.set_Item(1, (Int16)12);
            objs.set_Item(2, (Single)2.341);
            for (int iCtr = 0; iCtr < objs.length; iCtr++) {
                Console.WriteLine(objs[iCtr]);
            }
        }
        catch (System.ArrayTypeMismatchException exp) {
            // ArrayTypeMismatchException is not thrown this time.
            System.Console.WriteLine("Exception Thrown.");
        }
    } //main
} //Class1 

System.Object
   System.Exception
     System.SystemException
      System.ArrayTypeMismatchException
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.