System.NotSupportedException Class

namespace System

Represents an operation that is not supported.

Description

The System.NotSupportedException class is a member of the System namespace. It is thrown when an invoked method or operation is not supported by the object or the underlying platform.

This exception is typically thrown by library code to indicate that a specific feature or operation is not implemented or cannot be performed under the current circumstances. For example, if a collection is designed to be read-only, attempting to add an element to it would throw a NotSupportedException.

Inheritance Hierarchy

Object
Exception
SystemException
System.NotSupportedException

Constructors

Constructors

  • NotSupportedException() public NotSupportedException()

    Initializes a new instance of the NotSupportedException class.

  • NotSupportedException(string message) public NotSupportedException(string message)

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

    Parameters:
    message: The error message that explains the reason for the exception.

  • NotSupportedException(string message, Exception innerException) public NotSupportedException(string message, Exception innerException)

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

    Parameters:
    message: The error message that explains the reason for the exception.
    innerException: The exception that is the cause of the current exception. If innerException is not null, the current exception is raised in a catch block that handles the inner exception.

Usage Example

Here's a simple example demonstrating how NotSupportedException might be thrown:


using System;

public class ReadOnlyList<T>
{
    private T[] _items;

    public ReadOnlyList(T[] items)
    {
        _items = items;
    }

    public T this[int index]
    {
        get { return _items[index]; }
    }

    public void Add(T item)
    {
        // This operation is not supported for a read-only list
        throw new NotSupportedException("Adding items is not supported for this read-only list.");
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        var list = new ReadOnlyList<int>(new int[] { 1, 2, 3 });

        try
        {
            list.Add(4); // This will throw NotSupportedException
        }
        catch (NotSupportedException ex)
        {
            Console.WriteLine($"Caught exception: {ex.Message}");
            // You can also log the full exception details:
            // Console.WriteLine(ex.ToString());
        }
    }
}
            

See Also