System.DllNotFoundException Class

Assembly: Mscorlib.dll
Namespace: System
Summary
The exception that is thrown when a DLL specified in a DLL import cannot be found.
C# Syntax:
[Serializable]
public class DllNotFoundException : TypeLoadException
Remarks
DllNotFoundException uses the HRESULT COR_E_DLLNOTFOUND, which has the value 0x80131524.

For a list of initial property values for an instance of DllNotFoundException, see the DllNotFoundException.#ctor constructors.

See also:
System Namespace | Exception | MSDN: handlingthrowingexceptions

System.DllNotFoundException Member List:

Public Constructors
ctor #1 Overloaded:
.ctor()

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Initializes a new instance of the DllNotFoundException class with default properties.
ctor #2 Overloaded:
.ctor(string message)

Initializes a new instance of the DllNotFoundException class with a specified error message.
ctor #4 Overloaded:
.ctor(string message, Exception inner)

Initializes a new instance of the DllNotFoundException class with a specified error message and a reference to the inner exception that is the cause of this exception.
Public Properties
HelpLink
(inherited from System.Exception)
Read-write

See base class member description: System.Exception.HelpLink


Gets or sets a link to the help file associated with this exception.
InnerException
(inherited from System.Exception)
Read-only

See base class member description: System.Exception.InnerException


Gets the Exception instance that caused the current exception.
Message
(inherited from System.TypeLoadException)
Read-only

See base class member description: System.TypeLoadException.Message


Gets the error message for this exception.
Source
(inherited from System.Exception)
Read-write

See base class member description: System.Exception.Source


Gets or sets the name of the application or the object that causes the error.
StackTrace
(inherited from System.Exception)
Read-only

See base class member description: System.Exception.StackTrace


Gets a string representation of the frames on the call stack at the time the current exception was thrown.
TargetSite
(inherited from System.Exception)
Read-only

See base class member description: System.Exception.TargetSite


Gets the method that throws the current exception.
TypeName
(inherited from System.TypeLoadException)
Read-only

See base class member description: System.TypeLoadException.TypeName


Gets the fully qualified name of the type that causes the exception.
Public Methods
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetBaseException
(inherited from System.Exception)
See base class member description: System.Exception.GetBaseException


When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetObjectData
(inherited from System.TypeLoadException)
See base class member description: System.TypeLoadException.GetObjectData


Sets the SerializationInfo object with the class name, method name, resource ID, and additional exception information.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToString
(inherited from System.Exception)
See base class member description: System.Exception.ToString


Creates and returns a string representation of the current exception.
Protected Constructors
ctor #3 Overloaded:
.ctor(SerializationInfo info, StreamingContext context)

Initializes a new instance of the DllNotFoundException class with serialized data.
Protected Properties
HResult
(inherited from System.Exception)
Read-write

See base class member description: System.Exception.HResult


Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.DllNotFoundException Member Details

Overloaded ctor #1
Summary
Initializes a new instance of the DllNotFoundException class with default properties.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public DllNotFoundException();
Remarks
The following table shows the initial property values for an instance of DllNotFoundException.

Property Value
Exception.InnerException null .
TypeLoadException.Message The localized error message string.

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of the DllNotFoundException class with a specified error message.
C# Syntax:
public DllNotFoundException(
   string message
);
Parameters:

message

The error message that explains the reason for the exception.

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

Property Value
Exception.InnerException null .
TypeLoadException.Message The error message string.

Return to top


Overloaded ctor #3
Summary
Initializes a new instance of the DllNotFoundException class with serialized data.
C# Syntax:
protected DllNotFoundException(
   SerializationInfo info,
   StreamingContext context
);
Parameters:

info

The SerializationInfo that holds the serialized object data about the exception being thrown.

context

The StreamingContext that contains contextual information about the source or destination.

Return to top


Overloaded ctor #4
Summary
Initializes a new instance of the DllNotFoundException class with a specified error message and a reference to the inner exception that is the cause of this exception.
C# Syntax:
public DllNotFoundException(
   string message,
   Exception inner
);
Parameters:

message

The error message that explains the reason for the exception.

inner

The exception that is the cause of the current exception. If the inner parameter is not null, 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 should include a reference to the previous exception in the Exception.InnerException property. The Exception.InnerException property returns the same value that is passed into the constructor, or null if the Exception.InnerException property does not supply the inner exception value to the constructor.

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



Property Value
Exception.InnerException The inner exception reference.
TypeLoadException.Message The error message string.
See also:
Exception | MSDN: handlingthrowingexceptions

Return to top


Property: HelpLink (read-write)
Inherited
See base class member description: System.Exception.HelpLink

Summary
Gets or sets a link to the help file associated with this exception.
C# Syntax:
public virtual string HelpLink {get; set;}
Remarks
The return value, which represents a help file, is a URN or URL. For example, the HelpLink value could be:

"file:///C:/Applications/Bazzal/help.html#ErrorNum42"

Return to top


Property: HResult (read-write)
Inherited
See base class member description: System.Exception.HResult

Summary
Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.
C# Syntax:
protected int HResult {get; set;}
Remarks
HRESULT is a 32-bit value, divided into three different fields: a severity code, a facility code, and an error code. The severity code indicates whether the return value represents information, warning, or error. The facility code identifies the area of the system responsible for the error. The error code is a unique number that is assigned to represent the exception. Each exception is mapped to a distinct HRESULT. When managed code throws an exception, the runtime passes the HRESULT to the COM client. When unmanaged code returns an error, the HRESULT is converted to an exception, which is then thrown by the runtime.

Return to top


Property: InnerException (read-only)
Inherited
See base class member description: System.Exception.InnerException

Summary
Gets the Exception instance that caused the current exception.
C# Syntax:
public Exception InnerException {get;}
Remarks
When an exception X is thrown as a direct result of a previous exception Y, the InnerException property of X should contain a reference to Y.

Use the InnerException property to obtain the set of exceptions that led to the current exception.

You can create a new exception that catches an earlier exception. The code that handles the second exception can make use of the additional information from the earlier exception to handle the error more appropriately.

Suppose that there is a function that reads a file and formats the data from that file. In this example, as the code tries to read the file, an IOException is thrown. The function catches the IOException and throws a FileNotFoundException. The IOException could be saved in the Exception.InnerException property of the FileNotFoundException, enabling the code that catches the FileNotFoundException to examine what causes the initial error.

The Exception.InnerException property, which holds a reference to the inner exception, is set upon initialization of the exception object.

Example
The following example demonstrates throwing and catching an exception that references an inner exception.
using System;
public class MyAppException:ApplicationException 
{
   public MyAppException (String message) : base (message) 
   {}
   public MyAppException (String message, Exception inner) : base(message,inner) {}	
   }
public class ExceptExample 
{
   public void ThrowInner () 
   {
   throw new MyAppException("ExceptExample inner exception");
   }
   public void CatchInner() 
   {
      try 
      {
      this.ThrowInner();
      }
         catch (Exception e) 
         {
         throw new MyAppException("Error caused by trying ThrowInner.",e);
         }
      }
   }
public class Test 
{
   public static void Main() 
   {
   ExceptExample testInstance = new ExceptExample();
      try 
      {
      testInstance.CatchInner();
      }
         catch(Exception e) 
         {
         Console.WriteLine ("In Main catch block. Caught: {0}", e.Message);
         Console.WriteLine ("Inner Exception is {0}",e.InnerException);
         }
      }
}

    

This code has the following output:

In Main catch block. Caught: Error caused by trying ThrowInner. Inner Exception is MyAppException: ExceptExample inner exception at ExceptExample.ThrowInner() at ExceptExample.CatchInner()

Return to top


Property: Message (read-only)
Inherited
See base class member description: System.TypeLoadException.Message

Summary
Gets the error message for this exception.
C# Syntax:
public override string Message {get;}
Remarks
This property overrides Exception.Message. The error message should be localized.

This proerty is read-only.

Return to top


Property: Source (read-write)
Inherited
See base class member description: System.Exception.Source

Summary
Gets or sets the name of the application or the object that causes the error.
C# Syntax:
public virtual string Source {get; set;}
Remarks
If Exception.Source is not set, the name of the assembly where the exception originated is returned.

Return to top


Property: StackTrace (read-only)
Inherited
See base class member description: System.Exception.StackTrace

Summary
Gets a string representation of the frames on the call stack at the time the current exception was thrown.
C# Syntax:
public virtual string StackTrace {get;}
Remarks
The execution stack keeps track of all the methods that are in execution at a given instant. A trace of the method calls is called a stack trace. The stack trace listing provides a means to follow the call sequence to the line number in the method where the exception occurs.

StackTrace may not report as many method calls as expected, due to code transformations, such as inlining, that occur during optimization.



Notes to inheritors: The StackTrace property is overridden in classes that require control over the stack trace content or format.

By default, the stack trace is captured immediately before an exception object is thrown. Use Environment.StackTrace to get stack trace information when no exception is being thrown.

See also:
Environment.StackTrace

Return to top


Property: TargetSite (read-only)
Inherited
See base class member description: System.Exception.TargetSite

Summary
Gets the method that throws the current exception.
C# Syntax:
public MethodBase TargetSite {get;}
Remarks
If the method that throws this exception is not available and the stack trace is not a null reference (not set or empty), Exception.TargetSite obtains the method from the stack trace. If the stack trace is a null reference , Exception.TargetSite also returns a null reference.

Return to top


Property: TypeName (read-only)
Inherited
See base class member description: System.TypeLoadException.TypeName

Summary
Gets the fully qualified name of the type that causes the exception.
C# Syntax:
public string TypeName {get;}
Remarks


Notes to inheritors: When overriding TypeLoadException.TypeName in a derived class, be sure to call the base class's TypeLoadException.TypeName property. This property is read-only.
Example
      Assembly myAssembly = Assembly.Load("mscorlib");
      try 
      {
         Console.WriteLine("This program throws an exception upon successful run.");
         // Attempt to load a non-existent type from an assembly. 
         Type myType = myAssembly.GetType("System.NonExistentType", true);
      }  
      catch (TypeLoadException e) 
      {
         // Display the name of the Type that was not found.
         Console.WriteLine ("TypeLoadException: \n\tError loading the type '{0}' from the assembly 'mscorlib'", e.TypeName);
         Console.WriteLine ("\tError Message = " + e.Message);
      }  
      catch (Exception e) 
      {
         Console.WriteLine ("Exception: Error Message = " + e.Message);
      }

    

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~DllNotFoundException();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetBaseException()
Inherited
See base class member description: System.Exception.GetBaseException

Summary
When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.
C# Syntax:
public virtual Exception GetBaseException();
Return Value:
The first exception thrown in a chain of exceptions. If the Exception.InnerException property of the current exception is a null reference (not set or empty), this property returns the current exception.
Remarks
A chain of exceptions consists of a set of exceptions such that each exception in the chain was thrown as a direct result of the exception referenced in its InnerException property. For a given chain, there can be exactly one exception that is the root cause of all other exceptions in the chain. This exception is called the base exception and its InnerException property always contains a null reference.

For all exceptions in a chain of exceptions, the GetBaseException method must return the same object (the base exception).

Use the GetBaseException method when you want to find the root cause of an exception but do not need information about exceptions that may have occurred between the current exception and the first exception.



Notes to inheritors: The GetBaseException method is overridden in classes that require control over the exception content or format.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetObjectData(
   SerializationInfo info,
   StreamingContext context
)
Inherited
See base class member description: System.TypeLoadException.GetObjectData

Summary
Sets the SerializationInfo object with the class name, method name, resource ID, and additional exception information.
C# Syntax:
public override void GetObjectData(
   SerializationInfo info,
   StreamingContext context
);
Parameters:

info

The object that holds the serialized object data.

context

The contextual information about the source or destination.

Exceptions
Exception Type Condition
ArgumentNullException The info object is null.
Implements:
ISerializable.GetObjectData
Remarks
TypeLoadException.GetObjectData sets a SerializationInfo with all the exception object data targeted for serialization. During deserialization, the exception object is reconstituted from the SerializationInfo transmitted over the stream.

For more information, see the conceptual topic at MSDN: serialization.

Example

using System;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap; 
using System.IO;

class GetObjectDataDemo : Attribute
{
   public static void Main()
   {
      // Load the mscorlib assembly and get a reference to it.
      Assembly myAssembly = Assembly.Load("mscorlib");
      try
      {
         Console.WriteLine ("Attempting to load a type not present in the assembly 'mscorlib'");
         // This loading of invalid type raises a TypeLoadException
         Type myType = myAssembly.GetType("System.NonExistentType", true);
      }         
      catch (TypeLoadException)
      {
         // Serialize the exception to disk and reconstitute it back again.
         try 
         {
            System.DateTime ErrorDatetime = DateTime.Now;
            Console.WriteLine("A TypeLoadException has been raised.");

            // Create MyTypeLoadException instance with current time.
            MyTypeLoadExceptionChild myTypeLoadExceptionChild = new MyTypeLoadExceptionChild(ErrorDatetime);
            IFormatter myFormatter = new SoapFormatter();
            Stream myFileStream = new FileStream("typeload.xml", FileMode.Create, FileAccess.Write, FileShare.None);
            Console.WriteLine("Serializing the TypeLoadException with DateTime as " + ErrorDatetime);

            // Serialize the MyTypeLoadException instance to a file.
            myFormatter.Serialize(myFileStream, myTypeLoadExceptionChild);
            myFileStream.Close();
            Console.WriteLine("Deserializing the Exception.");
            myFileStream = new FileStream("typeload.xml", FileMode.Open, FileAccess.Read, FileShare.None);

            // Deserialize and reconstitute the instance from file.
            myTypeLoadExceptionChild = (MyTypeLoadExceptionChild) myFormatter.Deserialize(myFileStream);
            myFileStream.Close();
            Console.WriteLine("Deserialized exception has ErrorDateTime = " + myTypeLoadExceptionChild.ErrorDateTime);
         }
         catch (Exception e)
         {
            Console.WriteLine ("Exception :" + e.Message);
         }
      }
      catch(Exception e)
      {
         Console.WriteLine("Exception : " + e.Message);
      }
   }
}

// This class overrides the GetObjectData method and initializes
// its data with current time. 

[Serializable]
public class MyTypeLoadExceptionChild : TypeLoadException 
{
   public System.DateTime ErrorDateTime = DateTime.Now;
   public MyTypeLoadExceptionChild(DateTime myDateTime) 
   {
      ErrorDateTime = myDateTime;
   }

   protected MyTypeLoadExceptionChild(SerializationInfo sInfo, StreamingContext sContext)
   {
      // Reconstitute the deserialized information into the instance.
      ErrorDateTime=sInfo.GetDateTime("ErrorDate");
   }

   public override void GetObjectData(SerializationInfo sInfo, StreamingContext sContext) 
   {
      // Add a value to the Serialization information.
      sInfo.AddValue("ErrorDate",ErrorDateTime);
   }
}

    
See also:
SerializationInfo | MSDN: serialization

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Exception.ToString

Summary
Creates and returns a string representation of the current exception.
C# Syntax:
public override string ToString();
Return Value:
A string representation of the current exception.
Remarks
ToString returns a representation of the current exception that is intended to be understood by humans. Where the exception contains culture-sensitive data, the string representation returned by ToString is required to take into account the current system culture. Although there are no exact requirements for the format of the returned string, it should attempt to reflect the value of the object as perceived by the user.

The default implementation of Exception.ToString obtains the name of the class that threw the current exception, the message, the result of calling ToString on the inner exception, and the result of calling Environment.StackTrace. If any of these members is a null reference (not set or empty), its value is not included in the returned string.

If there is no error message or if it is an empty string (""), then no error message is returned. The name of the inner exception and the stack trace are returned only if they are not a null reference.

This method overrides Object.ToString.



Notes to inheritors: It is recommended, but not required, that ToString be overridden to return information about members declared in the derived class. For example, the ArgumentException class implements ToString so that it returns the value of the ParamName property, if that value is not a null reference.
Example
The following example causes an exception and displays the result of calling ToString on that exception.
using System;

public class MyClass {}
public class ArgExceptionExample 
   {
   public static void Main()
      {
      MyClass my = new MyClass();
      string s = "sometext";
      try 
         {
         int i = s.CompareTo(my);
         }
            catch (Exception e) 
            {
            Console.WriteLine("Error: {0}",e.ToString());
            }
      }
}

    

This code has the following output:

Error: System.ArgumentException: Object must be of type String. at System.String.CompareTo(Object value) at ArgExceptionExample.Main()

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.