Can't use std::exception because it dynamically allocates memory. This implementation doesn't allocate memory, but also doesn't allow leaving an exception message.
115 lines
2.2 KiB
C++
115 lines
2.2 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
|
|
namespace mtl {
|
|
|
|
/**
|
|
* \brief Generic exception interface
|
|
*
|
|
* Unlike the std::exception, this implementation does not use a what() method
|
|
* that returns a string. Instead, each derived exception provides an exception
|
|
* ID that can be queried through id(). IDs 0-100 are reserved.
|
|
*
|
|
* Corresponding exception IDs:
|
|
*
|
|
* invalid_argument: 10
|
|
* domain_error: 11
|
|
* length_error: 12
|
|
* out_of_range: 13
|
|
*
|
|
* range_error: 20
|
|
* overflow_error: 21
|
|
* underflow_error: 22
|
|
* system_error: 23
|
|
*/
|
|
class exception {
|
|
uint32_t m_id;
|
|
|
|
public:
|
|
exception(uint32_t id) noexcept : m_id(id) {}
|
|
uint32_t id() const noexcept { return m_id; }
|
|
};
|
|
|
|
/**
|
|
* \brief Thrown when an invalid argument is passed
|
|
*
|
|
* Exception ID: 10
|
|
*/
|
|
class invalid_argument : public exception {
|
|
public:
|
|
invalid_argument() noexcept : exception(10) {}
|
|
};
|
|
|
|
/**
|
|
* \brief Thrown when an input is outside the domain of an operation
|
|
*
|
|
* Exception ID: 11
|
|
*/
|
|
class domain_error : public exception {
|
|
public:
|
|
domain_error() noexcept : exception(11) {}
|
|
};
|
|
|
|
/**
|
|
* \brief Thrown when an internal length limit is exceeded
|
|
*
|
|
* Exception ID: 12
|
|
*/
|
|
class length_error : public exception {
|
|
public:
|
|
length_error() noexcept : exception(12) {}
|
|
};
|
|
|
|
/**
|
|
* \brief Thrown when an index is supplied that is outside a valid range
|
|
*
|
|
* Exception ID: 13
|
|
*/
|
|
class out_of_range : public exception {
|
|
public:
|
|
out_of_range() noexcept : exception(13) {}
|
|
};
|
|
|
|
/**
|
|
* \brief Thrown when a value cannot be represented by the destination type
|
|
*
|
|
* Exception ID: 20
|
|
*/
|
|
class range_error : public exception {
|
|
public:
|
|
range_error() noexcept : exception(20) {}
|
|
};
|
|
|
|
/**
|
|
* \brief Thrown when an arithmetic overflow occurs
|
|
*
|
|
* Exception ID: 21
|
|
*/
|
|
class overflow_error : public exception {
|
|
public:
|
|
overflow_error() noexcept : exception(21) {}
|
|
};
|
|
|
|
/**
|
|
* \brief Thrown when an arithmetic underflow occurs
|
|
*
|
|
* Exception ID: 22
|
|
*/
|
|
class underflow_error : public exception {
|
|
public:
|
|
underflow_error() noexcept : exception(22) {}
|
|
};
|
|
|
|
/**
|
|
* \brief Thrown when a system error occurs
|
|
*
|
|
* Exception ID: 23
|
|
*/
|
|
class system_error : public exception {
|
|
public:
|
|
system_error() noexcept : exception(23) {}
|
|
};
|
|
|
|
}; // namespace mtl
|