LibrePCB Developers Documentation
exceptions.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_CORE_EXCEPTIONS_H
21 #define LIBREPCB_CORE_EXCEPTIONS_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include <QtCore>
27 
28 /*******************************************************************************
29  * Namespace / Forward Declarations
30  ******************************************************************************/
31 namespace librepcb {
32 
33 class FilePath;
34 
35 /*******************************************************************************
36  * Class Exception
37  ******************************************************************************/
38 
84 class Exception : public QException {
85 public:
86  // Constructors / Destructor
87 
91  Exception() = delete;
92 
96  Exception(const Exception& other) noexcept;
97 
108  Exception(const char* file, int line,
109  const QString& msg = QString("Exception")) noexcept;
110 
114  virtual ~Exception() noexcept {}
115 
116  // Getters
117 
123  const QString& getMsg() const { return mMsg; }
124 
130  const QString& getFile() const { return mFile; }
131 
137  int getLine() const { return mLine; }
138 
150  const char* what() const noexcept override;
151 
152  // Inherited from QException (see QException documentation for more details)
153  virtual void raise() const override { throw *this; }
154  virtual Exception* clone() const override { return new Exception(*this); }
155 
156 private:
157  // Attributes
158  QString mMsg;
159  QString mFile;
160  int mLine;
161 
162  // Cached Attributes
163  mutable QByteArray mMsgUtf8;
164 };
165 
166 /*******************************************************************************
167  * Class LogicError
168  ******************************************************************************/
169 
179 class LogicError final : public Exception {
180 public:
184  LogicError() = delete;
185 
189  LogicError(const char* file, int line,
190  const QString& msg = QString("Logic Error")) noexcept;
191 
195  LogicError(const LogicError& other) noexcept;
196 
197  // Inherited from Exception
198  virtual void raise() const override { throw *this; }
199  virtual LogicError* clone() const override { return new LogicError(*this); }
200 };
201 
202 /*******************************************************************************
203  * Class RuntimeError
204  ******************************************************************************/
205 
216 class RuntimeError : public Exception {
217 public:
221  RuntimeError() = delete;
222 
226  RuntimeError(const char* file, int line,
227  const QString& msg = QString("Runtime Error")) noexcept;
228 
232  RuntimeError(const RuntimeError& other) noexcept;
233 
237  virtual ~RuntimeError() noexcept {}
238 
239  // Inherited from Exception
240  virtual void raise() const override { throw *this; }
241  virtual RuntimeError* clone() const override {
242  return new RuntimeError(*this);
243  }
244 };
245 
246 /*******************************************************************************
247  * Class RangeError
248  ******************************************************************************/
249 
257 class RangeError final : public RuntimeError {
258 public:
262  RangeError() = delete;
263 
267  RangeError(const char* file, int line,
268  const QString& msg = QString("Range Error")) noexcept;
269 
279  template <typename Tval, typename Tmin, typename Tmax>
280  RangeError(const char* file, int line, const Tval& value, const Tmin& min,
281  const Tmax& max) noexcept
282  : RangeError(file, line,
283  QString("Range error: %1 not in [%2..%3]")
284  .arg(value)
285  .arg(min)
286  .arg(max)) {}
287 
291  RangeError(const RangeError& other) noexcept;
292 
293  // Inherited from RuntimeError
294  virtual void raise() const override { throw *this; }
295  virtual RangeError* clone() const override { return new RangeError(*this); }
296 };
297 
298 /*******************************************************************************
299  * Class FileParseError
300  ******************************************************************************/
301 
310 class FileParseError final : public RuntimeError {
311 public:
315  FileParseError() = delete;
316 
329  FileParseError(const char* file, int line, const FilePath& filePath,
330  int fileLine = -1, int fileColumn = -1,
331  const QString& invalidFileContent = QString(),
332  const QString& msg = QString("File Parse Error")) noexcept;
333 
337  FileParseError(const FileParseError& other) noexcept;
338 
339  // Inherited from RuntimeError
340  virtual void raise() const override { throw *this; }
341  virtual FileParseError* clone() const override {
342  return new FileParseError(*this);
343  }
344 };
345 
346 /*******************************************************************************
347  * Class UserCanceled
348  ******************************************************************************/
349 
373 class UserCanceled final : public Exception {
374 public:
378  UserCanceled() = delete;
379 
383  UserCanceled(const char* file, int line,
384  const QString& msg = QString("User Canceled")) noexcept;
385 
389  UserCanceled(const UserCanceled& other) noexcept;
390 
391  // Inherited from Exception
392  virtual void raise() const override { throw *this; }
393  virtual UserCanceled* clone() const override {
394  return new UserCanceled(*this);
395  }
396 };
397 
398 /*******************************************************************************
399  * End of File
400  ******************************************************************************/
401 
402 } // namespace librepcb
403 
404 #endif
virtual RangeError * clone() const override
Definition: exceptions.h:295
The LogicError class.
Definition: exceptions.h:179
RangeError(const char *file, int line, const Tval &value, const Tmin &min, const Tmax &max) noexcept
Definition: exceptions.h:280
virtual UserCanceled * clone() const override
Definition: exceptions.h:393
Exception()=delete
The default constructor.
int getLine() const
Get the line number where the exception was thrown.
Definition: exceptions.h:137
virtual LogicError * clone() const override
Definition: exceptions.h:199
const QString & getMsg() const
Get the error message (translated)
Definition: exceptions.h:123
Definition: occmodel.cpp:77
The Exception class.
Definition: exceptions.h:84
int mLine
the line number where the exception was thrown
Definition: exceptions.h:160
virtual Exception * clone() const override
Definition: exceptions.h:154
const char * what() const noexcept override
reimplemented from std::exception::what()
Definition: exceptions.cpp:49
QString mMsg
the error message (translated)
Definition: exceptions.h:158
The UserCanceled class.
Definition: exceptions.h:373
The RangeError class.
Definition: exceptions.h:257
The RuntimeError class.
Definition: exceptions.h:216
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:129
QByteArray mMsgUtf8
the message as an UTF8 byte array
Definition: exceptions.h:163
The FileParseError class.
Definition: exceptions.h:310
const QString & getFile() const
Get the source file where the exception was thrown.
Definition: exceptions.h:130
virtual RuntimeError * clone() const override
Definition: exceptions.h:241
virtual FileParseError * clone() const override
Definition: exceptions.h:341
virtual ~Exception() noexcept
The destructor.
Definition: exceptions.h:114
virtual ~RuntimeError() noexcept
Destructor.
Definition: exceptions.h:237
QString mFile
the source filename where the exception was thrown
Definition: exceptions.h:159