Files
stdex/include/stdex/exception.hpp
2025-06-17 16:45:09 +02:00

89 lines
1.7 KiB
C++

/*
SPDX-License-Identifier: MIT
Copyright © 2023-2025 Amebis
*/
#pragma once
#include "compat.hpp"
#include <stdexcept>
#include <string>
namespace stdex
{
///
/// User cancelled exception
///
class user_cancelled : public std::runtime_error
{
public:
///
/// Constructs an exception
///
/// \param[in] msg Error message
///
user_cancelled(_In_opt_z_ const char* msg = "operation cancelled") : runtime_error(msg) {}
};
///
/// Concatenates error messages when exception is nested.
///
/// \param ex Exception
///
/// \returns Concatenated exception messages with ": " delimiter.
///
inline std::string exception_msg(const std::exception& ex)
{
try { std::rethrow_if_nested(ex); }
catch (const std::exception& nested_ex) {
return std::string(ex.what()) + ": " + exception_msg(nested_ex);
}
catch (...) {}
return ex.what();
}
///
/// Numerical runtime error
///
template <typename _Tn>
class num_runtime_error : public std::runtime_error
{
public:
typedef _Tn error_type; ///< Error number type
public:
///
/// Constructs an exception
///
/// \param[in] num Numeric error code
/// \param[in] msg Error message
///
num_runtime_error(_In_ error_type num, _In_ const std::string& msg) :
m_num(num),
runtime_error(msg)
{}
///
/// Constructs an exception
///
/// \param[in] num Numeric error code
/// \param[in] msg Error message
///
num_runtime_error(_In_ error_type num, _In_z_ const char *msg) :
m_num(num),
runtime_error(msg)
{}
///
/// Returns the error number
///
error_type number() const
{
return m_num;
}
protected:
error_type m_num; ///< Numeric error code
};
}