Table of Contents

UniqueIndexViolationException Class

Definition

Namespace
Cuemon.Data
Assemblies
Cuemon.Data.dll
Source
src/Cuemon.Data/UniqueIndexViolationException.cs

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.

// Program.cs
using System;
using Cuemon.Data;

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

Name Description
UniqueIndexViolationException()

Initializes a new instance of the UniqueIndexViolationException class.

UniqueIndexViolationException(string)

Initializes a new instance of the UniqueIndexViolationException class.

UniqueIndexViolationException(string, Exception)

Initializes a new instance of the UniqueIndexViolationException class.