Exceptions

Exception hierarchy, throwing conventions, C# to C++ mapping, and handling patterns.

System::Exception — The Base

include/System/Exception.hpp | src/System/Exception.cpp

System::Exception inherits from std::exception, giving it full integration with standard C++ exception handling:

class Exception : public std::exception {
public:
    Exception();
    explicit Exception(const char* msg);
    explicit Exception(const std::string& msg);
    virtual const std::string& getMessageProperty() const;
    const char* what() const noexcept override;
private:
    std::string message_;
};

Note: System::Exception does not inherit from System::Object. This keeps it compatible with std::exception and standard C++ exception mechanisms.

Exception Hierarchy

std::exception
└── System::Exception
      ├── System::SystemException
      │     ├── ArgumentException
      │     │     ├── ArgumentNullException
      │     │     └── ArgumentOutOfRangeException
      │     ├── ArithmeticException
      │     │     ├── DivideByZeroException
      │     │     └── OverflowException
      │     ├── FormatException
      │     ├── IndexOutOfRangeException
      │     ├── InvalidCastException
      │     ├── InvalidOperationException
      │     ├── NotImplementedException
      │     ├── NotSupportedException
      │     ├── NullReferenceException
      │     ├── ObjectDisposedException
      │     ├── OperationCanceledException
      │     ├── OutOfMemoryException
      │     ├── TimeoutException
      │     ├── UnauthorizedAccessException
      │     ├── IO::IOException
      │     │     ├── IO::DirectoryNotFoundException
      │     │     ├── IO::EndOfStreamException
      │     │     ├── IO::FileNotFoundException
      │     │     ├── IO::InvalidDataException
      │     │     └── IO::IsolatedStorage::IsolatedStorageException
      │     └── ... (many more)
      └── System::ApplicationException

Complete Exception List

Exception ClassHeaderC# Equivalent
ExceptionSystem/Exception.hppSystem.Exception
SystemExceptionSystem/SystemException.hppSystem.SystemException
ApplicationExceptionSystem/ApplicationException.hppSystem.ApplicationException
ArgumentExceptionSystem/ArgumentException.hppSystem.ArgumentException
ArgumentNullExceptionSystem/ArgumentNullException.hppSystem.ArgumentNullException
ArgumentOutOfRangeExceptionSystem/ArgumentOutOfRangeException.hppSystem.ArgumentOutOfRangeException
ArithmeticExceptionSystem/ArithmeticException.hppSystem.ArithmeticException
DivideByZeroExceptionSystem/DivideByZeroException.hppSystem.DivideByZeroException
OverflowExceptionSystem/OverflowException.hppSystem.OverflowException
FormatExceptionSystem/FormatException.hppSystem.FormatException
IndexOutOfRangeExceptionSystem/IndexOutOfRangeException.hppSystem.IndexOutOfRangeException
InvalidCastExceptionSystem/InvalidCastException.hppSystem.InvalidCastException
InvalidOperationExceptionSystem/InvalidOperationException.hppSystem.InvalidOperationException
NotImplementedExceptionSystem/NotImplementedException.hppSystem.NotImplementedException
NotSupportedExceptionSystem/NotSupportedException.hppSystem.NotSupportedException
NullReferenceExceptionSystem/NullReferenceException.hppSystem.NullReferenceException
ObjectDisposedExceptionSystem/ObjectDisposedException.hppSystem.ObjectDisposedException
OperationCanceledExceptionSystem/OperationCanceledException.hppSystem.OperationCanceledException
OutOfMemoryExceptionSystem/OutOfMemoryException.hppSystem.OutOfMemoryException
TimeoutExceptionSystem/TimeoutException.hppSystem.TimeoutException
UnauthorizedAccessExceptionSystem/UnauthorizedAccessException.hppSystem.UnauthorizedAccessException
PlatformNotSupportedExceptionSystem/PlatformNotSupportedException.hppSystem.PlatformNotSupportedException
IO::IOExceptionSystem/IO/IOException.hppSystem.IO.IOException
IO::FileNotFoundExceptionSystem/IO/FileNotFoundException.hppSystem.IO.FileNotFoundException
IO::DirectoryNotFoundExceptionSystem/IO/DirectoryNotFoundException.hppSystem.IO.DirectoryNotFoundException
IO::EndOfStreamExceptionSystem/IO/EndOfStreamException.hppSystem.IO.EndOfStreamException
IO::InvalidDataExceptionSystem/IO/InvalidDataException.hppSystem.IO.InvalidDataException
Collections::Generic::KeyNotFoundExceptionSystem/Collections/Generic/KeyNotFoundException.hppSystem.Collections.Generic.KeyNotFoundException

Throwing Conventions

// Throw by value (standard C++ practice)
throw System::ArgumentNullException("Parameter 'name' cannot be null.");
throw System::ArgumentOutOfRangeException("index", "Index out of range.");
throw System::NotImplementedException("This method is not yet implemented.");
throw System::PlatformNotSupportedException("Not supported on this platform.");

Catching Conventions

try {
    // ...
}
catch (const System::ArgumentNullException& e) {
    // specific catch
    std::cerr << e.getMessageProperty() << "\n";
}
catch (const System::Exception& e) {
    // catch any sharp-runtime exception
    std::cerr << e.what() << "\n";
}
catch (const std::exception& e) {
    // catch std or sharp-runtime exceptions
    std::cerr << e.what() << "\n";
}

Stub Exceptions

Several exception types exist purely for API compatibility but are rarely thrown. They have headers but minimal or empty .cpp bodies:

Known Limitations vs .NET