CryptographicException Class

 

The exception that is thrown when an error occurs during a cryptographic operation.

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

System::Object
  System::Exception
    System::SystemException
      System.Security.Cryptography::CryptographicException
        System.Security.Cryptography::CryptographicUnexpectedOperationException

[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class CryptographicException : SystemException

NameDescription
System_CAPS_pubmethodCryptographicException()

Initializes a new instance of the CryptographicException class with default properties.

System_CAPS_pubmethodCryptographicException(Int32)

Initializes a new instance of the CryptographicException class with the specified HRESULT error code.

System_CAPS_protmethodCryptographicException(SerializationInfo^, StreamingContext)

Initializes a new instance of the CryptographicException class with serialized data.

System_CAPS_pubmethodCryptographicException(String^)

Initializes a new instance of the CryptographicException class with a specified error message.

System_CAPS_pubmethodCryptographicException(String^, Exception^)

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

System_CAPS_pubmethodCryptographicException(String^, String^)

Initializes a new instance of the CryptographicException class with a specified error message in the specified format.

NameDescription
System_CAPS_pubpropertyData

Gets a collection of key/value pairs that provide additional user-defined information about the exception.(Inherited from Exception.)

System_CAPS_pubpropertyHelpLink

Gets or sets a link to the help file associated with this exception.(Inherited from Exception.)

System_CAPS_pubpropertyHResult

Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.(Inherited from Exception.)

System_CAPS_pubpropertyInnerException

Gets the Exception instance that caused the current exception.(Inherited from Exception.)

System_CAPS_pubpropertyMessage

Gets a message that describes the current exception.(Inherited from Exception.)

System_CAPS_pubpropertySource

Gets or sets the name of the application or the object that causes the error.(Inherited from Exception.)

System_CAPS_pubpropertyStackTrace

Gets a string representation of the immediate frames on the call stack.(Inherited from Exception.)

System_CAPS_pubpropertyTargetSite

Gets the method that throws the current exception.(Inherited from Exception.)

NameDescription
System_CAPS_pubmethodEquals(Object^)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetBaseException()

When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.(Inherited from Exception.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetObjectData(SerializationInfo^, StreamingContext)

When overridden in a derived class, sets the SerializationInfo with information about the exception.(Inherited from Exception.)

System_CAPS_pubmethodGetType()

Gets the runtime type of the current instance.(Inherited from Exception.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodToString()

Creates and returns a string representation of the current exception.(Inherited from Exception.)

NameDescription
System_CAPS_proteventSerializeObjectState

Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.(Inherited from Exception.)

CryptographicException uses the HRESULT CORSEC_E_CRYPTO, unless an alternate error code is specified.

For a list of initial property values for an instance of CryptographicException, see the constructor.

The following code example demonstrates how to use members of the CryptographicException class.

using namespace System;
using namespace System::Text;
using namespace System::Security::Cryptography;
using namespace System::Runtime::Serialization;

ref class CryptographicExceptionMembers
{
public:

   static void Run()
   {
      CryptographicExceptionMembers^ testRun = gcnew CryptographicExceptionMembers;
      testRun->TestConstructors();
      testRun->ShowProperties();
      Console::WriteLine( L"This sample ended successfully; "
      L" press Enter to exit." );
      Console::ReadLine();
   }

private:
   // Test each public implementation of the CryptographicException
   // constructors.
   void TestConstructors()
   {
      EmptyConstructor();
      IntConstructor();
      StringConstructor();
      StringExceptionConstructor();
      StringStringConstructor();
   }

   void EmptyConstructor()
   {
      // Construct a CryptographicException with no parameters.
      CryptographicException^ cryptographicException = gcnew CryptographicException;

      Console::WriteLine( L"Created an empty CryptographicException." );
   }

   void IntConstructor()
   {
      // Construct a CryptographicException using the error code for an
      // unexpected operation exception.
      int exceptionNumber = (int)0x80131431;
      CryptographicException^ cryptographicException = gcnew CryptographicException( exceptionNumber );

      Console::WriteLine( L"Created a CryptographicException with the "
      L"following error code: {0}", exceptionNumber );
   }

   void StringConstructor()
   {
      // Construct a CryptographicException using a custom error message.
      String^ errorMessage = (L"Unexpected Operation exception.");
      CryptographicException^ cryptographicException = gcnew CryptographicException( errorMessage );

      Console::WriteLine( L"Created a CryptographicException with the "
      L"following error message: {0}", errorMessage );
   }

   void StringExceptionConstructor()
   {
      // Construct a CryptographicException using a custom error message
      // and an inner exception.
      String^ errorMessage = (L"The current operation is not supported.");
      NullReferenceException^ nullException = gcnew NullReferenceException;
      CryptographicException^ cryptographicException = gcnew CryptographicException( errorMessage,nullException );

      Console::WriteLine( L"Created a CryptographicException with the "
      L"following error message: {0} and the inner exception of {1}", errorMessage, nullException );
   }

   void StringStringConstructor()
   {
      // Create a CryptographicException using a time format and a the
      // current date.
      String^ dateFormat = L"{0:t}";
      String^ timeStamp = (DateTime::Now.ToString());
      CryptographicException^ cryptographicException = gcnew CryptographicException( dateFormat,timeStamp );

      Console::WriteLine( L"Created a CryptographicException with ({0}) as the format and ({1}) as the message.", dateFormat, timeStamp );
   }

   // Construct an invalid DSACryptoServiceProvider to throw a
   // CryptographicException for introspection.
   void ShowProperties()
   {
      try
      {
         // Create a DSACryptoServiceProvider with invalid provider type
         // code to throw a CryptographicException exception.
         CspParameters^ cspParams = gcnew CspParameters( 44 );
         DSACryptoServiceProvider^ DSAalg = gcnew DSACryptoServiceProvider( cspParams );
      }
      catch ( CryptographicException^ ex ) 
      {
         // Retrieve the link to the help file for the exception.
         String^ helpLink = ex->HelpLink;

         // Retrieve the exception that caused the current
         // CryptographicException exception.
         System::Exception^ innerException = ex->InnerException;

         String^ innerExceptionMessage = L"";
         if ( innerException != nullptr )
         {
            innerExceptionMessage = innerException->ToString();
         }

         // Retrieve the message that describes the exception.
         String^ message = ex->Message;

         // Retrieve the name of the application that caused the exception.
         String^ exceptionSource = ex->Source;

         // Retrieve the call stack at the time the exception occured.
         String^ stackTrace = ex->StackTrace;

         // Retrieve the method that threw the exception.
         System::Reflection::MethodBase^ targetSite = ex->TargetSite;

         String^ siteName = targetSite->Name;

         // Retrieve the entire exception as a single string.
         String^ entireException = ex->ToString();

         // GetObjectData
         setSerializationInfo(  &ex );

         // Get the root exception that caused the current
         // CryptographicException exception.
         System::Exception^ baseException = ex->GetBaseException();

         String^ baseExceptionMessage = L"";
         if ( baseException != nullptr )
         {
            baseExceptionMessage = baseException->Message;
         }
         Console::WriteLine( L"Caught an expected exception:" );
         Console::WriteLine( entireException );
         Console::WriteLine( L"\n" );
         Console::WriteLine( L"Properties of the exception are as follows:" );
         Console::WriteLine( L"Message: {0}", message );
         Console::WriteLine( L"Source: {0}", exceptionSource );
         Console::WriteLine( L"Stack trace: {0}", stackTrace );
         Console::WriteLine( L"Help link: {0}", helpLink );
         Console::WriteLine( L"Target site's name: {0}", siteName );
         Console::WriteLine( L"Base exception message: {0}", baseExceptionMessage );
         Console::WriteLine( L"Inner exception message: {0}", innerExceptionMessage );
      }

   }

   void setSerializationInfo( interior_ptr<CryptographicException^> ex )
   {
      // Insert information about the exception into a serialized object.
      FormatterConverter^ formatConverter = gcnew FormatterConverter;
      SerializationInfo^ serializationInfo = gcnew SerializationInfo( ( *ex)->GetType(),formatConverter );
      StreamingContext streamingContext = StreamingContext(StreamingContextStates::All);
      ( *ex)->GetObjectData( serializationInfo, streamingContext );
   }

};

void main()
{
   CryptographicExceptionMembers::Run();
}

//
// This sample produces the following output:
//
// Created an empty CryptographicException.
// Created a CryptographicException with the following error code: -2146233295
// Created a CryptographicException with the following error message:
// Unexpected Operation exception.
// Created a CryptographicException with the following error message: The
// current operation is not supported. and the inner exception of
// System.NullReferenceException: Object reference not set to an instance of
// an object.
// Created a CryptographicException with ({0:t}) as the format and (2/24/2004
// 2:13:15 PM) as the message.
// Caught an expected exception:
// System.Security.Cryptography.CryptographicException: CryptoAPI
// cryptographic service provider (CSP) for this implementation could not be
// acquired.
//  at System.Security.Cryptography.DSACryptoServiceProvider..ctor(Int32
// dwKeySize, CspParameters parameters)
//  at System.Security.Cryptography.DSACryptoServiceProvider..ctor(
// CspParametersparameters)
//  at CryptographicExceptionMembers.ShowProperties() in c:\inetpub\
// vssolutions\test\testbuild\consoleapplication1\class1.cs:line 109
//
//
// Properties of the exception are as follows:
// Message: CryptoAPI cryptographic service provider (CSP) for this
// implementation could not be acquired.
// Source: mscorlib
// Stack trace:
//  at System.Security.Cryptography.DSACryptoServiceProvider..ctor(
// Int32 dwKeySize, CspParameters parameters)
//  at System.Security.Cryptography.DSACryptoServiceProvider..ctor(
// CspParameters parameters)
//  at CryptographicExceptionMembers.ShowProperties() in c:\inetpub\
// vssolutions\test\testbuild\consoleapplication1\class1.cs:line 109
// Help link:
// Target site's name: .ctor
// Base exception message: CryptoAPI cryptographic service provider (CSP) for
// this implementation could not be acquired.
// Inner exception message:
// This sample ended successfully;  press Enter to exit.

.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0

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

Return to top
Show: