Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

CryptographicUnexpectedOperationException Class

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

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

Namespace:  System.Security.Cryptography
Assembly:  mscorlib (in mscorlib.dll)
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class CryptographicUnexpectedOperationException : CryptographicException

The CryptographicUnexpectedOperationException type exposes the following members.

  NameDescription
Public methodCryptographicUnexpectedOperationException()Initializes a new instance of the CryptographicUnexpectedOperationException class with default properties.
Public methodCryptographicUnexpectedOperationException(String)Initializes a new instance of the CryptographicUnexpectedOperationException class with a specified error message.
Protected methodCryptographicUnexpectedOperationException(SerializationInfo, StreamingContext)Initializes a new instance of the CryptographicUnexpectedOperationException class with serialized data.
Public methodCryptographicUnexpectedOperationException(String, Exception)Initializes a new instance of the CryptographicUnexpectedOperationException class with a specified error message and a reference to the inner exception that is the cause of this exception.
Public methodCryptographicUnexpectedOperationException(String, String)Initializes a new instance of the CryptographicUnexpectedOperationException class with a specified error message in the specified format.
Top
  NameDescription
Public propertyDataGets a collection of key/value pairs that provide additional user-defined information about the exception. (Inherited from Exception.)
Public propertyHelpLinkGets or sets a link to the help file associated with this exception. (Inherited from Exception.)
Public propertyHResultGets or sets HRESULT, a coded numerical value that is assigned to a specific exception. (Inherited from Exception.)
Public propertyInnerExceptionGets the Exception instance that caused the current exception. (Inherited from Exception.)
Public propertyMessageGets a message that describes the current exception. (Inherited from Exception.)
Public propertySourceGets or sets the name of the application or the object that causes the error. (Inherited from Exception.)
Public propertyStackTraceGets a string representation of the immediate frames on the call stack. (Inherited from Exception.)
Public propertyTargetSiteGets the method that throws the current exception. (Inherited from Exception.)
Top
  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetBaseExceptionWhen overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions. (Inherited from Exception.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetObjectDataWhen overridden in a derived class, sets the SerializationInfo with information about the exception. (Inherited from Exception.)
Public methodGetTypeGets the runtime type of the current instance. (Inherited from Exception.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringCreates and returns a string representation of the current exception. (Inherited from Exception.)
Top
  NameDescription
Protected eventSerializeObjectStateOccurs when an exception is serialized to create an exception state object that contains serialized data about the exception. (Inherited from Exception.)
Top

CryptographicUnexpectedOperationException uses the HRESULT CORSEC_E_CRYPTO_UNEX_OPER, which has the value 0x80131431.

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

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

using System;
using System.Security.Cryptography;
using System.Runtime.Serialization;

class Members
{
    [STAThread]
    static void Main(string[] args)
    {
        Members testRun = new Members();

        testRun.TestConstructors();
        testRun.ShowProperties();

        Console.WriteLine("Sample ended successfully, " +
            " press Enter to continue.");
        Console.ReadLine();
    }

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

    private void EmptyConstructor()
    {
        // Construct a CryptographicUnexpectedOperationException 
        // with no parameters.
        CryptographicUnexpectedOperationException cryptographicException =
            new CryptographicUnexpectedOperationException();
        Console.WriteLine("Created an empty " + 
            "CryptographicUnexpectedOperationException.");
    }

    private void StringConstructor()
    {
        // Construct a CryptographicUnexpectedOperationException 
        // using a custom error message. 
        string errorMessage = ("Unexpected operation exception.");
        CryptographicUnexpectedOperationException cryptographicException =
            new CryptographicUnexpectedOperationException(errorMessage);
        Console.WriteLine("Created a " + 
            "CryptographicUnexpectedOperationException with the following " +
            " error message: " + errorMessage);
    }

    private void StringExceptionConstructor()
    {
        // Construct a CryptographicUnexpectedOperationException using a  
        // custom error message and an inner exception. 
        string errorMessage = ("The current operation is not supported.");
        NullReferenceException nullException = new NullReferenceException();
        CryptographicUnexpectedOperationException cryptographicException = 
            new CryptographicUnexpectedOperationException(
            errorMessage, nullException);
        Console.WriteLine("Created a " +
            "CryptographicUnexpectedOperationException with the following" +
            "error message: " + errorMessage + " and inner exception: "
            + nullException.ToString());
    }

    private void StringStringConstructor()
    {
        // Create a CryptographicUnexpectedOperationException using a time 
        // format and the current date. 
        string dateFormat = "{0:t}";
        string timeStamp = (DateTime.Now.ToString());
        CryptographicUnexpectedOperationException cryptographicException = 
            new CryptographicUnexpectedOperationException(
            dateFormat, timeStamp);
        Console.WriteLine("Created a " + 
            "CryptographicUnexpectedOperationException with (" + dateFormat +
            ") as the format and (" + timeStamp + ") as the message.");
    }

    // Construct an invalid DSACryptoServiceProvider to throw a 
    // CryptographicUnexpectedOperationException for introspection. 
    private void ShowProperties()
    {
        // Attempting to encode an OID greater than 127 bytes is not supported 
        // and will throw an exception. 
        string veryLongNumber = "1234567890.1234567890.";
        for (int i=0; i < 4; i++)
        {
            veryLongNumber += veryLongNumber;
        }
        veryLongNumber += "0";
        try 
        {
            byte[] tooLongOID = CryptoConfig.EncodeOID(veryLongNumber);
        }
        catch(CryptographicUnexpectedOperationException ex)
        {
            // Retrieve the link to the Help file for the exception. 
            string helpLink = ex.HelpLink;

            // Retrieve the exception that caused the current 
            // CryptographicUnexpectedOperationException.
            System.Exception innerException = ex.InnerException;
            string innerExceptionMessage = "";
            if (innerException != null)
            {
                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 occurred. 
            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(ref ex);

            // Get the root exception that caused the current 
            // CryptographicUnexpectedOperationException.
            System.Exception baseException = ex.GetBaseException();
            string baseExceptionMessage = "";
            if (baseException != null)
            {
                baseExceptionMessage = baseException.Message;
            }

            Console.WriteLine("Caught an expected exception:");
            Console.WriteLine(entireException + "\n");

            Console.WriteLine("Properties of the exception are as follows:");
            Console.WriteLine("Message: " + message);
            Console.WriteLine("Source: " + exceptionSource);
            Console.WriteLine("Stack trace: " + stackTrace);
            Console.WriteLine("Help link: " + helpLink);
            Console.WriteLine("Target site's name: " + siteName);
            Console.WriteLine("Base exception message: " + 
                baseExceptionMessage);
            Console.WriteLine("Inner exception message: " + 
                innerExceptionMessage);
        }
    }

    private void setSerializationInfo(
        ref CryptographicUnexpectedOperationException ex)
    {
        // Insert information about the exception into a serialized object.
        FormatterConverter formatConverter = new FormatterConverter();
        SerializationInfo serializationInfo =
            new SerializationInfo(ex.GetType(), formatConverter);
        StreamingContext streamingContext =
            new StreamingContext(StreamingContextStates.All);

        ex.GetObjectData(serializationInfo,streamingContext);
    }
}
// 
// This sample produces the following output: 
// 
// Created an empty CryptographicUnexpectedOperationException. 
// Created a CryptographicUnexpectedOperationException with the following  
// error message: Unexpected operation exception. 
// Created a CryptographicUnexpectedOperationException with the following 
// error message: The current operation is not supported. and inner exception: 
// System.NullReferenceException: Object reference not set to an instance of 
// an object. 
// Created a CryptographicUnexpectedOperationException with ({0:t}) as the 
// format and (2/24/2004 2:35:22 PM) as the message. 
// Caught an expected exception: 
// System.Security.Cryptography.CryptographicUnexpectedOperationException:  
// Encoded OID length is too large (greater than 0x7f bytes). 
//  at System.Security.Cryptography.CryptoConfig.EncodeOID(String str) 
//  at Members.ShowProperties() in c:\consoleapplication1\class1.cs:line 106 
//  
// Properties of the exception are as follows: 
// Message: Encoded OID length is too large (greater than 0x7f bytes). 
// Source: mscorlib 
// Stack trace:    at System.Security.Cryptography.CryptoConfig.EncodeOID( 
// String str) 
//  at Members.ShowProperties() in c:\consoleapplication1\class1.cs:line 106 
// Help link: 
// Target site's name: EncodeOID 
// Base exception message: Encoded OID length is too large (greater than 0x7f 
// bytes). 
// Inner exception message: 
// Sample ended successfully,  press Enter to continue.

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.