mirror of
https://github.com/dankamongmen/notcurses.git
synced 2024-10-31 15:20:13 +00:00
64eeb95f1e
Nick prefers error handling based on exceptions in all cases, while I prefer to save exception handling for truly exceptional situations - function parameter validation and class constructor. However, there's no need to not support both approaches, to be chosen at the discretion of the developer. NCPP follows RAII and all classes throw exceptions from their constructors in case they cannot initialize properly. Likewise, functions taking pointers that are required validate them and throw exceptions whenever the requirement isn't met. This commit goes one step further in that it enables optional validation of notcurses function return values and throwing an exception (`ncpp::call_error`) should the function signal an error. This is disabled by default but it can be enabled by defining the `NCPP_EXCEPTIONS_PLEASE` macro (preferably on the command line or before *each* inclusion of any NCPP headers). Out of necessity, this breaks the ABI (plus I found a handful of minor issues in the code), but I think it's worth having this support in place.
59 lines
1.1 KiB
C++
59 lines
1.1 KiB
C++
#ifndef __NCPP_EXCEPTIONS_HH
|
|
#define __NCPP_EXCEPTIONS_HH
|
|
|
|
#include <stdexcept>
|
|
|
|
#include "_helpers.hh"
|
|
|
|
namespace ncpp
|
|
{
|
|
class NCPP_API_EXPORT init_error : public std::logic_error
|
|
{
|
|
public:
|
|
explicit init_error (const std::string& what_arg)
|
|
: logic_error (what_arg)
|
|
{}
|
|
|
|
explicit init_error (const char* what_arg)
|
|
: logic_error (what_arg)
|
|
{}
|
|
};
|
|
|
|
class NCPP_API_EXPORT invalid_state_error : public std::logic_error
|
|
{
|
|
public:
|
|
explicit invalid_state_error (const std::string& what_arg)
|
|
: logic_error (what_arg)
|
|
{}
|
|
|
|
explicit invalid_state_error (const char* what_arg)
|
|
: logic_error (what_arg)
|
|
{}
|
|
};
|
|
|
|
class NCPP_API_EXPORT invalid_argument : public std::invalid_argument
|
|
{
|
|
public:
|
|
explicit invalid_argument (const std::string& what_arg)
|
|
: std::invalid_argument (what_arg)
|
|
{}
|
|
|
|
explicit invalid_argument (const char* what_arg)
|
|
: std::invalid_argument (what_arg)
|
|
{}
|
|
};
|
|
|
|
class NCPP_API_EXPORT call_error : public std::logic_error
|
|
{
|
|
public:
|
|
explicit call_error (const std::string& what_arg)
|
|
: logic_error (what_arg)
|
|
{}
|
|
|
|
explicit call_error (const char* what_arg)
|
|
: logic_error (what_arg)
|
|
{}
|
|
};
|
|
}
|
|
#endif
|