Quantum++  v1.0.0-beta2
C++11 quantum computing library
exception.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2017 Vlad Gheorghiu (vgheorgh@gmail.com)
5  *
6  * This file is part of Quantum++.
7  *
8  * Quantum++ is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * Quantum++ is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with Quantum++. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
27 #ifndef CLASSES_EXCEPTION_H_
28 #define CLASSES_EXCEPTION_H_
29 
30 namespace qpp
31 {
32 
39 class Exception : public std::exception
40 {
41 public:
46  enum class Type // exception types
47  {
50  ZERO_SIZE,
110  NO_CODEWORD,
113  OUT_OF_RANGE,
123  };
124 
131  Exception(const std::string& where, const Type& type) :
132  where_{where}, msg_{}, type_{type}, custom_{}
133  {
135  }
136 
145  Exception(const std::string& where, const std::string& custom) :
147  custom_{custom}
148  {
150  msg_ += custom; // add the custom message at the end
151  }
152 
158  virtual const char* what() const noexcept override
159  {
160  return msg_.c_str();
161  }
162 
163 private:
164  std::string where_, msg_;
166  std::string custom_;
167 
175  {
176  msg_ += "IN ";
177  msg_ += where_;
178  msg_ += ": ";
179 
180  switch (type_)
181  {
183  msg_ += "UNKNOWN EXCEPTION!";
184  break;
185  case Type::ZERO_SIZE:
186  msg_ += "Object has zero size!";
187  break;
189  msg_ += "Matrix is not square!";
190  break;
192  msg_ += "Matrix is not column vector!";
193  break;
195  msg_ += "Matrix is not row vector!";
196  break;
198  msg_ += "Matrix is not vector!";
199  break;
201  msg_ += "Matrix is not square nor column vector!";
202  break;
204  msg_ += "Matrix is not square nor row vector!";
205  break;
207  msg_ += "Matrix is not square nor vector!";
208  break;
210  msg_ += "Matrix mismatch subsystems!";
211  break;
212  case Type::DIMS_INVALID:
213  msg_ += "Invalid dimension(s)!";
214  break;
216  msg_ += "Dimensions not equal!";
217  break;
219  msg_ += "Dimension(s) mismatch matrix size!";
220  break;
222  msg_ += "Dimension(s) mismatch column vector!";
223  break;
225  msg_ += "Dimension(s) mismatch row vector!";
226  break;
228  msg_ += "Dimension(s) mismatch vector!";
229  break;
231  msg_ += "Subsystems mismatch dimensions!";
232  break;
233  case Type::PERM_INVALID:
234  msg_ += "Invalid permutation!";
235  break;
237  msg_ += "Permutation mismatch dimensions!";
238  break;
240  msg_ += "Matrix is not 2 x 2!";
241  break;
243  msg_ += "Column vector is not 2 x 1!";
244  break;
246  msg_ += "Row vector is not 1 x 2!";
247  break;
249  msg_ += "Vector is not 2 x 1 nor 1 x 2!";
250  break;
252  msg_ += "Subsystems are not qubits!";
253  break;
254  case Type::NOT_BIPARTITE:
255  msg_ += "Not bi-partite!";
256  break;
257  case Type::NO_CODEWORD:
258  msg_ += "Codeword does not exist!";
259  break;
260  case Type::OUT_OF_RANGE:
261  msg_ += "Parameter out of range!";
262  break;
263  case Type::TYPE_MISMATCH:
264  msg_ += "Type mismatch!";
265  break;
266  case Type::SIZE_MISMATCH:
267  msg_ += "Size mismatch!";
268  break;
270  msg_ += "Not defined for this type!";
271  break;
273  msg_ += "CUSTOM EXCEPTION ";
274  break;
275  }
276  }
277 }; /* class Exception */
278 
279 } /* namespace qpp */
280 
281 #endif /* CLASSES_EXCEPTION_H_ */
Type type_
Definition: exception.h:165
std::string custom_
Definition: exception.h:166
Quantum++ main namespace.
Definition: codes.h:30
Exception(const std::string &where, const std::string &custom)
Constructs an exception.
Definition: exception.h:145
Type
Exception types, add more here if needed.
Definition: exception.h:46
std::string where_
Definition: exception.h:164
Generates custom exceptions, used when validating function parameters.
Definition: exception.h:39
std::string msg_
Definition: exception.h:164
Exception(const std::string &where, const Type &type)
Constructs an exception.
Definition: exception.h:131
virtual const char * what() const noexcept override
Overrides std::exception::what()
Definition: exception.h:158
void construct_exception_msg_()
Constructs the exception description from its type.
Definition: exception.h:174