NSUNI/NSLAR Library a250670
Loading...
Searching...
No Matches
exception.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <string>
10
11#include "NNL/common/src_info.hpp"
12
20
39#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND))
40#define NNL_THROW(object) throw object
41#define NNL_TRY try
42#define NNL_CATCH(type) catch (const type& e)
43#else
44#include "NNL/common/panic.hpp" // IWYU pragma: export
45
46#define NNL_THROW(object) \
47 { nnl::Panic(object.what()); }
48
49#define NNL_TRY if (true)
50
51#define NNL_CATCH(type) else if (type e; false)
52#endif
53
55
56namespace nnl {
57
62
66class Exception : public std::exception {
67 public:
68 Exception(std::string what_arg = "") : m_(std::move(what_arg)) {}
69
70 const char* what() const noexcept override { return m_.c_str(); }
71
72 virtual ~Exception() = default;
73
74 protected:
75 static std::string Name(const std::string& ename) { return "[nnl." + ename + "] "; }
76 const std::string m_;
77};
78
82class RuntimeError : public Exception {
83 public:
84 RuntimeError(const std::string& what_arg = "") : Exception(Exception::Name("runtime_error") + what_arg) {}
85};
86
90class ParseError : public Exception {
91 public:
92 ParseError(const std::string& what_arg = "") : Exception(Exception::Name("parse_error") + what_arg) {}
93};
94
103class PreconditionError : public Exception {
104 public:
105 PreconditionError(const std::string& what_arg = "") : Exception(Exception::Name("precondition_error") + what_arg) {}
106};
107
111class RangeError : public Exception {
112 public:
113 RangeError(const std::string& what_arg = "") : Exception(Exception::Name("range_error") + what_arg) {}
114};
115
121class NarrowError : public Exception {
122 public:
123 NarrowError(const std::string& what_arg = "") : Exception(Exception::Name("narrow_error") + what_arg) {}
124};
125
131
132class IOError : public Exception {
133 public:
134 IOError(const std::string& what_arg = "") : Exception(Exception::Name("io_error") + what_arg) {}
135
136 IOError(const std::string& what_arg, const std::string& path)
137 : Exception(Exception::Name("io_error") + path + ": " + what_arg) {}
138};
139
140} // namespace nnl
Definition exception.hpp:56
Provides functions to set up the callback for fatal library errors.