OpenVDB 10.0.1
Loading...
Searching...
No Matches
Exceptions.h
Go to the documentation of this file.
1// Copyright Contributors to the OpenVDB Project
2// SPDX-License-Identifier: MPL-2.0
3
4#ifndef OPENVDB_EXCEPTIONS_HAS_BEEN_INCLUDED
5#define OPENVDB_EXCEPTIONS_HAS_BEEN_INCLUDED
6
7#include "version.h"
8#include <exception>
9#include <sstream>
10#include <string>
11
12
13namespace openvdb {
16
17class OPENVDB_API Exception: public std::exception
18{
19public:
20 Exception(const Exception&) = default;
21 Exception(Exception&&) = default;
22 Exception& operator=(const Exception&) = default;
24 ~Exception() override = default;
25
26 const char* what() const noexcept override
27 {
28 try { return mMessage.c_str(); } catch (...) {}
29 return nullptr;
30 }
31
32protected:
33 Exception() noexcept {}
34 explicit Exception(const char* eType, const std::string* const msg = nullptr) noexcept
35 {
36 try {
37 if (eType) mMessage = eType;
38 if (msg) mMessage += ": " + (*msg);
39 } catch (...) {}
40 }
41
42private:
43 std::string mMessage;
44};
45
46
47#define OPENVDB_EXCEPTION(_classname) \
48class OPENVDB_API _classname: public Exception \
49{ \
50public: \
51 _classname() noexcept: Exception( #_classname ) {} \
52 explicit _classname(const std::string& msg) noexcept: Exception( #_classname , &msg) {} \
53}
54
55
66
67#undef OPENVDB_EXCEPTION
68
69
70} // namespace OPENVDB_VERSION_NAME
71} // namespace openvdb
72
73
74#define OPENVDB_THROW(exception, message) \
75{ \
76 std::string _openvdb_throw_msg; \
77 try { \
78 std::ostringstream _openvdb_throw_os; \
79 _openvdb_throw_os << message; \
80 _openvdb_throw_msg = _openvdb_throw_os.str(); \
81 } catch (...) {} \
82 throw exception(_openvdb_throw_msg); \
83} // OPENVDB_THROW
84
85#endif // OPENVDB_EXCEPTIONS_HAS_BEEN_INCLUDED
#define OPENVDB_API
Definition Platform.h:251
Definition Exceptions.h:56
Definition Exceptions.h:18
Exception() noexcept
Definition Exceptions.h:33
Exception(const char *eType, const std::string *const msg=nullptr) noexcept
Definition Exceptions.h:34
Exception(Exception &&)=default
Exception & operator=(const Exception &)=default
const char * what() const noexcept override
Definition Exceptions.h:26
Exception & operator=(Exception &&)=default
Exception(const Exception &)=default
~Exception() override=default
Definition Exceptions.h:57
Definition Exceptions.h:58
Definition Exceptions.h:59
Definition Exceptions.h:60
Definition Exceptions.h:61
Definition Exceptions.h:62
Definition Exceptions.h:63
Definition Exceptions.h:64
Definition Exceptions.h:65
Definition Exceptions.h:13
#define OPENVDB_EXCEPTION(_classname)
Definition Exceptions.h:47
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition version.h.in:121
#define OPENVDB_USE_VERSION_NAMESPACE
Definition version.h.in:212