次の方法で共有


TypeLoadException Constructor (String, Exception)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Initializes a new instance of the TypeLoadException class with a specified error message and a reference to the inner exception that is the cause of this exception.

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

Syntax

'Declaration
Public Sub New ( _
    message As String, _
    inner As Exception _
)
public TypeLoadException(
    string message,
    Exception inner
)

Parameters

  • message
    Type: System.String
    The error message that explains the reason for the exception.
  • inner
    Type: System.Exception
    The exception that is the cause of the current exception. If the inner parameter is not nulla null reference (Nothing in Visual Basic), the current exception is raised in a catch block that handles the inner exception.

Remarks

An exception that is thrown as a direct result of a previous exception can include a reference to the previous exception in the InnerException property. The InnerException property returns the same value that is passed into the constructor, or nulla null reference (Nothing in Visual Basic) if the InnerException property does not supply the inner exception value to the constructor.

The following table shows the initial property values for an instance of TypeLoadException.

Property

Value

InnerException

The inner exception reference.

Message

The error message string.

Examples

The following code example demonstrates the TypeLoadException(String, Exception) constructor. It contains a method that generates a TypeLoadException, catches that exception, and throws a new TypeLoadException with a custom message, including the original TypeLoadException as the inner exception.

Imports System.Runtime.InteropServices

Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text &= "Calling a method in a non-existent DLL which triggers a TypeLoadException." & vbCrLf
      Try
         TypeLoadExceptionDemoClass.GenerateException()
      Catch e As TypeLoadException
         outputBlock.Text &= ("TypeLoadException: " + ControlChars.Cr + ControlChars.Tab + "Error Message = " + e.Message) & vbCrLf
         outputBlock.Text &= ("TypeLoadException: " + ControlChars.Cr + ControlChars.Tab + "InnerException Message = " + e.InnerException.Message) & vbCrLf
      Catch e As Exception
         outputBlock.Text &= ("Exception: " + ControlChars.Cr + ControlChars.Tab + "Error Message = " + e.Message) & vbCrLf
      End Try
   End Sub 'Main
End Class 'TypeLoadException_Constructor3

Class TypeLoadExceptionDemoClass
   ' A call to this method will raise a TypeLoadException.
   Public Declare Sub NonExistentMethod Lib "NonExistentDLL.DLL" Alias "MethodNotExists" ()

   Public Shared Sub GenerateException()
      Try
         NonExistentMethod()
      Catch e As TypeLoadException
         ' Rethrow exception with the exception as inner exception
         Throw New TypeLoadException("This exception was raised due to a call to an invalid method.", e)
      End Try
   End Sub 'GenerateException
End Class 'TypeLoadExceptionDemoClass

using System;
using System.Runtime.InteropServices;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += "Calling a method in a non-existent DLL which triggers a TypeLoadException." + "\n";
      try
      {
         TypeLoadExceptionDemoClass.GenerateException();
      }
      catch (TypeLoadException e)
      {
         outputBlock.Text += "TypeLoadException: \n\tError Message = " + e.Message + "\n";
         outputBlock.Text += "TypeLoadException: \n\tInnerException Message = " + e.InnerException.Message + "\n";
      }
      catch (Exception e)
      {
         outputBlock.Text += "Exception: \n\tError Message = " + e.Message + "\n";
      }
   }
}

class TypeLoadExceptionDemoClass
{
   // A call to this method will raise a TypeLoadException.
   [DllImport("NonExistentDLL.DLL", EntryPoint = "MethodNotExists")]
   public static extern void NonExistentMethod();

   public static void GenerateException()
   {
      try
      {
         NonExistentMethod();
      }
      catch (TypeLoadException e)
      {
         // Rethrow exception with the exception as inner exception
         throw new TypeLoadException("This exception was raised due to a call to an invalid method.", e);
      }
   }
}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.