SPHinXsys  alpha version
exception.h
Go to the documentation of this file.
1 
7 #ifndef EXCEPTION_SIMBODY_H
8 #define EXCEPTION_SIMBODY_H
9 
10 
11 
12 #include "base_data_package.h"
13 
14 #include <iostream>
15 #include <string>
16 #include <cstdio>
17 #include <stdexcept>
18 #include <cassert>
19 
20 namespace SPH {
28  class Exception : public std::exception {
29  private:
30  std::string msg_;
31  std::string file_;
32  int line_;
33 public:
39  Exception(const std::string &aMsg="", const std::string &aFile="", int aLine=-1);
40 
46  Exception(const std::string& file, size_t line, const std::string& func);
50  Exception(const std::string& file, size_t line,
51  const std::string& func, const std::string& msg);
55  virtual ~Exception() throw() {}
56 
57  protected:
59  void addMessage(const std::string& msg);
60 
61  private:
62  void setNull();
63 
64  public:
65  void setMessage(const std::string &aMsg);
66  const char* getMessage() const;
67 
68  virtual void print(std::ostream &aOut) const;
69 
70  const char* what() const noexcept override;
71  };
72 
73  class InvalidArgument : public Exception {
74  public:
75  InvalidArgument(const std::string& file,
76  size_t line,
77  const std::string& func,
78  const std::string& msg = "") :
79  Exception(file, line, func) {
80  std::string mesg = "Invalid Argument. " + msg;
81 
82  addMessage(mesg);
83  }
84  };
85 
86  class InvalidCall : public Exception {
87  public:
88  InvalidCall(const std::string& file,
89  size_t line,
90  const std::string& func,
91  const std::string& msg = "") :
92  Exception(file, line, func) {
93  std::string mesg = "Invalid Call. " + msg;
94 
95  addMessage(mesg);
96  }
97  };
98 
100  public:
101  InvalidTemplateArgument(const std::string& file,
102  size_t line,
103  const std::string& func,
104  const std::string& msg) :
105  Exception(file, line, func) {
106  std::string mesg = "Invalid Template argument. " + msg;
107 
108  addMessage(mesg);
109  }
110  };
111 
112  class IndexOutOfRange : public Exception {
113  public:
114  IndexOutOfRange(const std::string& file,
115  size_t line,
116  const std::string& func,
117  size_t index,
118  size_t min,
119  size_t max) :
120  Exception(file, line, func) {
121  std::string msg = "min = " + std::to_string(min);
122  msg += " max = " + std::to_string(max);
123  msg += " index = " + std::to_string(index);
124 
125  addMessage(msg);
126  }
127  };
128 
129  class KeyNotFound : public Exception {
130  public:
131  KeyNotFound(const std::string& file,
132  size_t line,
133  const std::string& func,
134  const std::string& key) :
135  Exception(file, line, func) {
136  std::string msg = "Key '" + key + "' not found.";
137 
138  addMessage(msg);
139  }
140  };
141 }
142 #endif //EXCEPTION_SIMBODY_H
Definition: exception.h:73
Definition: exception.h:86
Definition: exception.h:99
Exception(const std::string &aMsg="", const std::string &aFile="", int aLine=-1)
Default Constructor.
Definition: exception.cpp:9
Definition: exception.h:112
virtual ~Exception()
Definition: exception.h:55
void addMessage(const std::string &msg)
Definition: exception.cpp:42
A class for basic exception functionality.
Definition: exception.h:28
Definition: exception.h:129
Definition: solid_body_supplementary.cpp:9