Quantum++  v0.6
C++11 quantum computing library
exception.h
Go to the documentation of this file.
1 /*
2  * Quantum++
3  *
4  * Copyright (c) 2013 - 2015 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,
121  };
122 
129  Exception(const std::string& where, const Type& type) :
130  _where{where}, _msg{}, _type{type}, _custom{}
131  {
132  _construct_exception_msg();
133  }
134 
143  Exception(const std::string& where, const std::string& custom) :
144  _where{where}, _msg{}, _type{Type::CUSTOM_EXCEPTION},
145  _custom{custom}
146  {
147  _construct_exception_msg();
148  _msg += custom; // add the custom message at the end
149  }
150 
156  virtual const char* what() const noexcept override
157  {
158  return _msg.c_str();
159  }
160 
161 private:
162  std::string _where, _msg;
163  Type _type;
164  std::string _custom;
165 
172  void _construct_exception_msg()
173  {
174  _msg += "IN ";
175  _msg += _where;
176  _msg += ": ";
177 
178  switch (_type)
179  {
181  _msg += "UNKNOWN EXCEPTION!";
182  break;
183  case Type::ZERO_SIZE:
184  _msg += "Object has zero size!";
185  break;
187  _msg += "Matrix is not square!";
188  break;
190  _msg += "Matrix is not column vector!";
191  break;
193  _msg += "Matrix is not row vector!";
194  break;
196  _msg += "Matrix is not vector!";
197  break;
199  _msg += "Matrix is not square nor column vector!";
200  break;
202  _msg += "Matrix is not square nor row vector!";
203  break;
205  _msg += "Matrix is not square nor vector!";
206  break;
208  _msg += "Matrix mismatch subsystems!";
209  break;
210  case Type::DIMS_INVALID:
211  _msg += "Invalid dimension(s)!";
212  break;
214  _msg += "Dimensions not equal!";
215  break;
217  _msg += "Dimension(s) mismatch matrix size!";
218  break;
220  _msg += "Dimension(s) mismatch column vector!";
221  break;
223  _msg += "Dimension(s) mismatch row vector!";
224  break;
226  _msg += "Dimension(s) mismatch vector!";
227  break;
229  _msg += "Subsystems mismatch dimensions!";
230  break;
231  case Type::PERM_INVALID:
232  _msg += "Invalid permutation!";
233  break;
235  _msg += "Permutation mismatch dimensions!";
236  break;
238  _msg += "Matrix is not 2 x 2!";
239  break;
241  _msg += "Column vector is not 2 x 1!";
242  break;
244  _msg += "Row vector is not 1 x 2!";
245  break;
247  _msg += "Vector is not 2 x 1 nor 1 x 2!";
248  break;
250  _msg += "Subsystems are not qubits!";
251  break;
252  case Type::NOT_BIPARTITE:
253  _msg += "Not bi-partite!";
254  break;
255  case Type::NO_CODEWORD:
256  _msg += "Codeword does not exist!";
257  break;
258  case Type::OUT_OF_RANGE:
259  _msg += "Parameter out of range!";
260  break;
261  case Type::TYPE_MISMATCH:
262  _msg += "Type mismatch!";
263  break;
265  _msg += "Not defined for this type!";
266  break;
268  _msg += "CUSTOM EXCEPTION ";
269  break;
270  }
271  }
272 }; /* class Exception */
273 
274 } /* namespace qpp */
275 
276 #endif /* CLASSES_EXCEPTION_H_ */
virtual const char * what() const noexceptoverride
Overrides std::exception::what()
Definition: exception.h:156
Quantum++ main namespace.
Definition: codes.h:30
Exception(const std::string &where, const std::string &custom)
Constructs an exception.
Definition: exception.h:143
Type
Exception types, add more here if needed.
Definition: exception.h:46
Generates custom exceptions, used when validating function parameters.
Definition: exception.h:39
Exception(const std::string &where, const Type &type)
Constructs an exception.
Definition: exception.h:129