SpinParser  1.0
Exception.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 #include <string>
11 #include <sstream>
12 #include <exception>
13 
17 struct Exception : public std::exception
18 {
19 public:
23  enum struct Type
24  {
25  GenericError,
28  OutOfRange,
31  IOError,
33  MpiError
34  };
35 
41  Exception(const Exception::Type type) noexcept
42  {
43  _type = type;
44  switch (type)
45  {
47  _what = "GenericError";
48  break;
50  _what = "InternalError";
51  break;
53  _what = "BadAllocation";
54  break;
56  _what = "OutOfRange";
57  break;
59  _what = "ArgumentError";
60  break;
62  _what = "InitializationError";
63  break;
65  _what = "IOError";
66  break;
68  _what = "AssertionFail";
69  break;
71  _what = "MpiError";
72  break;
73  default:
74  _what = "UnknownType";
75  break;
76  }
77  }
78 
86  template <class T> Exception(const Exception::Type type, T details) noexcept : Exception(type)
87  {
88  std::stringstream ss;
89  ss << "[" << _what << "]: " << details;
90  _what = ss.str();
91  }
92 
96  ~Exception() noexcept {};
97 
103  const char* what() const noexcept
104  {
105  return _what.c_str();
106  }
107 
113  Exception::Type type() const noexcept
114  {
115  return _type;
116  }
117 
118 protected:
120  std::string _what;
121 };
Exception::Type::InitializationError
@ InitializationError
Initialization error, raised when initialization routines fail.
Exception::Type::BadAllocation
@ BadAllocation
Allocation error, raised when an allocation fails.
Exception::Exception
Exception(const Exception::Type type, T details) noexcept
Construct a new Exception object.
Definition: Exception.hpp:86
Exception::type
Exception::Type type() const noexcept
Retrieve exception type.
Definition: Exception.hpp:113
Exception::Type::MpiError
@ MpiError
MPI error, raised when MPI communication fails.
Exception::Type::IOError
@ IOError
In/out error, raised when input or output routines fail.
Exception
Descriptor object for exceptions.
Definition: Exception.hpp:17
Exception::Type::AssertionFail
@ AssertionFail
Assertion failure, raised when an assertion fails.
Exception::_what
std::string _what
String form description of the exception.
Definition: Exception.hpp:120
Exception::Type::OutOfRange
@ OutOfRange
Out of range error, raised when out of bound memory accesses occur.
Exception::Exception
Exception(const Exception::Type type) noexcept
Construct a new Exception object.
Definition: Exception.hpp:41
Exception::what
const char * what() const noexcept
Retrieve string form description of the exception.
Definition: Exception.hpp:103
Exception::Type::ArgumentError
@ ArgumentError
Argument error, raised when a function is invoked with an invalid argument.
Exception::Type::InternalError
@ InternalError
Internal error, raised when internal inconsistencies occur.
Exception::Type::GenericError
@ GenericError
Generic error.
Exception::Type
Type
Exception types.
Definition: Exception.hpp:23
Exception::_type
Exception::Type _type
Type of the exception.
Definition: Exception.hpp:119
Exception::~Exception
~Exception() noexcept
Destroy the Exception object.
Definition: Exception.hpp:96