System.PlatformNotSupportedException Class

System

Represents the exception that is thrown when an attempt to run an unsupported operation is made. This is typically an API that is not implemented on the current platform.

Inheritance

Syntax

public sealed class PlatformNotSupportedException : NotSupportedException

Remarks

This exception is thrown when an API is called that is not implemented or supported on the current operating system or execution environment. For example, calling a file system operation on an embedded system that does not have a file system might throw this exception.

It is recommended to check for platform compatibility before attempting to use APIs that might not be supported. Use conditional compilation directives (e.g., #if NET6_0_OR_GREATER) or runtime checks to ensure your code runs correctly across different platforms.

Constructors

Example

C# Example

using System;

public class Example
{
    public static void Main(string[] args)
    {
        try
        {
            // Attempt to perform an operation that might not be supported
            // For demonstration purposes, we'll manually throw it.
            // In a real scenario, this would be an API call.
            throw new PlatformNotSupportedException("This feature is not available on the current platform.");
        }
        catch (PlatformNotSupportedException ex)
        {
            Console.WriteLine($"Caught a PlatformNotSupportedException:");
            Console.WriteLine($"  Message: {ex.Message}");
            Console.WriteLine($"  StackTrace: {ex.StackTrace}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Caught another exception: {ex.GetType().Name}");
        }
    }
}

Related Articles