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 Class | Header | C# Equivalent |
|---|---|---|
Exception | System/Exception.hpp | System.Exception |
SystemException | System/SystemException.hpp | System.SystemException |
ApplicationException | System/ApplicationException.hpp | System.ApplicationException |
ArgumentException | System/ArgumentException.hpp | System.ArgumentException |
ArgumentNullException | System/ArgumentNullException.hpp | System.ArgumentNullException |
ArgumentOutOfRangeException | System/ArgumentOutOfRangeException.hpp | System.ArgumentOutOfRangeException |
ArithmeticException | System/ArithmeticException.hpp | System.ArithmeticException |
DivideByZeroException | System/DivideByZeroException.hpp | System.DivideByZeroException |
OverflowException | System/OverflowException.hpp | System.OverflowException |
FormatException | System/FormatException.hpp | System.FormatException |
IndexOutOfRangeException | System/IndexOutOfRangeException.hpp | System.IndexOutOfRangeException |
InvalidCastException | System/InvalidCastException.hpp | System.InvalidCastException |
InvalidOperationException | System/InvalidOperationException.hpp | System.InvalidOperationException |
NotImplementedException | System/NotImplementedException.hpp | System.NotImplementedException |
NotSupportedException | System/NotSupportedException.hpp | System.NotSupportedException |
NullReferenceException | System/NullReferenceException.hpp | System.NullReferenceException |
ObjectDisposedException | System/ObjectDisposedException.hpp | System.ObjectDisposedException |
OperationCanceledException | System/OperationCanceledException.hpp | System.OperationCanceledException |
OutOfMemoryException | System/OutOfMemoryException.hpp | System.OutOfMemoryException |
TimeoutException | System/TimeoutException.hpp | System.TimeoutException |
UnauthorizedAccessException | System/UnauthorizedAccessException.hpp | System.UnauthorizedAccessException |
PlatformNotSupportedException | System/PlatformNotSupportedException.hpp | System.PlatformNotSupportedException |
IO::IOException | System/IO/IOException.hpp | System.IO.IOException |
IO::FileNotFoundException | System/IO/FileNotFoundException.hpp | System.IO.FileNotFoundException |
IO::DirectoryNotFoundException | System/IO/DirectoryNotFoundException.hpp | System.IO.DirectoryNotFoundException |
IO::EndOfStreamException | System/IO/EndOfStreamException.hpp | System.IO.EndOfStreamException |
IO::InvalidDataException | System/IO/InvalidDataException.hpp | System.IO.InvalidDataException |
Collections::Generic::KeyNotFoundException | System/Collections/Generic/KeyNotFoundException.hpp | System.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:
AccessViolationExceptionAppDomainUnloadedExceptionBadImageFormatExceptionExecutionEngineExceptionStackOverflowException
Known Limitations vs .NET
- No inner exception (
InnerExceptionproperty) — not visible in the current implementation - No stack trace capture —
StackTraceproperty is a stub - No unhandled exception event with real thread-pool integration
OutOfMemoryExceptionis not automatically thrown on allocation failure (C++std::bad_allocwould be thrown instead)