System::Exception
Base exception class. Inherits std::exception. Root of the exception hierarchy.
include/System/Exception.hpp
Status: Implemented
Overview
System::Exception is the base class for all exceptions thrown by sharp-runtime code.
It inherits std::exception, which means it integrates naturally with standard C++
try/catch and can be caught as std::exception&.
The message string is stored internally as std::string message_, and
what() returns a C-string pointer to it (satisfying std::exception).
Class Definition
namespace System {
class Exception : public std::exception {
public:
explicit Exception(const std::string& message = "");
virtual const char* what() const noexcept override;
virtual std::string getMessage() const;
virtual std::string GetTypeName() const; // from Object pattern
protected:
std::string message_;
};
}
Constructors
| Form | Description |
|---|---|
Exception() | Default — empty message |
Exception(const std::string& message) | Exception with a message string |
Methods
what()
virtual const char* what() const noexcept override;
Returns the message as a null-terminated C string. Satisfies std::exception. Delegates to message_.c_str().
getMessage()
virtual std::string getMessage() const;
Returns the message as std::string. Mirrors C# Exception.Message.
Usage
// Throwing
throw System::InvalidOperationException("Object is in invalid state");
// Catching as base type
try {
doSomething();
} catch (const System::Exception& ex) {
std::cerr << ex.what() << "\n";
}
// Catching as std::exception (also works)
try {
doSomething();
} catch (const std::exception& ex) {
std::cerr << ex.what() << "\n";
}
Exception Hierarchy
The full hierarchy is documented in Exceptions. The most common derived classes:
| Class | Header |
|---|---|
SystemException | System/SystemException.hpp |
ArgumentException | System/ArgumentException.hpp |
ArgumentNullException | System/ArgumentNullException.hpp |
ArgumentOutOfRangeException | System/ArgumentOutOfRangeException.hpp |
InvalidOperationException | System/InvalidOperationException.hpp |
NotImplementedException | System/NotImplementedException.hpp |
NotSupportedException | System/NotSupportedException.hpp |
NullReferenceException | System/NullReferenceException.hpp |
IndexOutOfRangeException | System/IndexOutOfRangeException.hpp |
IOException | System/IO/IOException.hpp |
OverflowException | System/OverflowException.hpp |
Creating Custom Exceptions
class MyException : public System::Exception {
public:
explicit MyException(const std::string& msg) : System::Exception(msg) {}
GetTypeNameHPP();
};
// In .cpp:
GetTypeNameCPP(MyException, "MyNamespace.MyException");
Differences from .NET
| .NET | sharp-runtime |
|---|---|
Exception.Message (property) | getMessage() method |
Exception.InnerException | Needs verification — may be stub |
Exception.StackTrace | Not implemented (StackTrace is stub) |
Inherits Object | Inherits std::exception instead |