Class UniqueIndexViolationException
The exception that is thrown when a unique index violation occurs from a data source.
public class UniqueIndexViolationException : Exception, ISerializable
- Inheritance
-
UniqueIndexViolationException
- Implements
- Inherited Members
Examples
UniqueIndexViolationException represents a unique index or constraint violation error, with support for inner exceptions and parameterless construction. This example throws a new instance with a descriptive message about a duplicate key in dbo.Users and catches it to print the message. It also creates a wrapped exception with an inner InvalidOperationException as the cause, and demonstrates that the default parameterless constructor has no inner exception.
using System;
using Cuemon.Data;
namespace MyApp.Data
{
public sealed class UniqueIndexViolationExceptionExample
{
public void Demonstrate()
{
try
{
throw new UniqueIndexViolationException("Cannot insert duplicate key row in object 'dbo.Users'.");
}
catch (UniqueIndexViolationException ex)
{
Console.WriteLine(ex.Message);
}
var wrapped = new UniqueIndexViolationException(
"Failed to register user.",
new InvalidOperationException("IX_Users_Email was violated."));
Console.WriteLine(wrapped.Message);
Console.WriteLine(wrapped.InnerException?.Message);
var empty = new UniqueIndexViolationException();
Console.WriteLine($"Default exception has no inner exception: {empty.InnerException is null}");
}
}
}
Constructors
UniqueIndexViolationException()
Initializes a new instance of the UniqueIndexViolationException class.
public UniqueIndexViolationException()
UniqueIndexViolationException(string)
Initializes a new instance of the UniqueIndexViolationException class.
public UniqueIndexViolationException(string message)
Parameters
messagestringThe message that describes the error.
UniqueIndexViolationException(string, Exception)
Initializes a new instance of the UniqueIndexViolationException class.
public UniqueIndexViolationException(string message, Exception innerException)