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

FormDescription
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:

ClassHeader
SystemExceptionSystem/SystemException.hpp
ArgumentExceptionSystem/ArgumentException.hpp
ArgumentNullExceptionSystem/ArgumentNullException.hpp
ArgumentOutOfRangeExceptionSystem/ArgumentOutOfRangeException.hpp
InvalidOperationExceptionSystem/InvalidOperationException.hpp
NotImplementedExceptionSystem/NotImplementedException.hpp
NotSupportedExceptionSystem/NotSupportedException.hpp
NullReferenceExceptionSystem/NullReferenceException.hpp
IndexOutOfRangeExceptionSystem/IndexOutOfRangeException.hpp
IOExceptionSystem/IO/IOException.hpp
OverflowExceptionSystem/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

.NETsharp-runtime
Exception.Message (property)getMessage() method
Exception.InnerExceptionNeeds verification — may be stub
Exception.StackTraceNot implemented (StackTrace is stub)
Inherits ObjectInherits std::exception instead