LibrePCB Developers Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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_EXCEPTIONS_H
21 #define LIBREPCB_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 
87 class Exception : public QException {
88 public:
89  // Constructors / Destructor
90 
94  Exception() = delete;
95 
99  Exception(const Exception& other) noexcept;
100 
111  Exception(const char* file, int line,
112  const QString& msg = QString("Exception")) noexcept;
113 
117  virtual ~Exception() noexcept {}
118 
119  // Getters
120 
126  const QString& getMsg() const { return mMsg; }
127 
133  const QString& getFile() const { return mFile; }
134 
140  int getLine() const { return mLine; }
141 
153  const char* what() const noexcept override;
154 
155  // Inherited from QException (see QException documentation for more details)
156  virtual void raise() const override { throw *this; }
157  virtual Exception* clone() const override { return new Exception(*this); }
158 
159 private:
160  // Attributes
161  QString mMsg;
162  QString mFile;
163  int mLine;
164 
165  // Cached Attributes
166  mutable QByteArray mMsgUtf8;
167 };
168 
169 /*******************************************************************************
170  * Class LogicError
171  ******************************************************************************/
172 
182 class LogicError final : public Exception {
183 public:
187  LogicError() = delete;
188 
192  LogicError(const char* file, int line,
193  const QString& msg = QString("Logic Error")) noexcept;
194 
198  LogicError(const LogicError& other) noexcept;
199 
200  // Inherited from Exception
201  virtual void raise() const override { throw *this; }
202  virtual LogicError* clone() const override { return new LogicError(*this); }
203 };
204 
205 /*******************************************************************************
206  * Class RuntimeError
207  ******************************************************************************/
208 
219 class RuntimeError : public Exception {
220 public:
224  RuntimeError() = delete;
225 
229  RuntimeError(const char* file, int line,
230  const QString& msg = QString("Runtime Error")) noexcept;
231 
235  RuntimeError(const RuntimeError& other) noexcept;
236 
240  virtual ~RuntimeError() noexcept {}
241 
242  // Inherited from Exception
243  virtual void raise() const override { throw *this; }
244  virtual RuntimeError* clone() const override {
245  return new RuntimeError(*this);
246  }
247 };
248 
249 /*******************************************************************************
250  * Class RangeError
251  ******************************************************************************/
252 
260 class RangeError final : public RuntimeError {
261 public:
265  RangeError() = delete;
266 
270  RangeError(const char* file, int line,
271  const QString& msg = QString("Range Error")) noexcept;
272 
282  template <typename Tval, typename Tmin, typename Tmax>
283  RangeError(const char* file, int line, const Tval& value, const Tmin& min,
284  const Tmax& max) noexcept
285  : RangeError(file, line,
286  QString("Range error: %1 not in [%2..%3]")
287  .arg(value)
288  .arg(min)
289  .arg(max)) {}
290 
294  RangeError(const RangeError& other) noexcept;
295 
296  // Inherited from RuntimeError
297  virtual void raise() const override { throw *this; }
298  virtual RangeError* clone() const override { return new RangeError(*this); }
299 };
300 
301 /*******************************************************************************
302  * Class FileParseError
303  ******************************************************************************/
304 
313 class FileParseError final : public RuntimeError {
314 public:
318  FileParseError() = delete;
319 
332  FileParseError(const char* file, int line, const FilePath& filePath,
333  int fileLine = -1, int fileColumn = -1,
334  const QString& invalidFileContent = QString(),
335  const QString& msg = QString("File Parse Error")) noexcept;
336 
340  FileParseError(const FileParseError& other) noexcept;
341 
342  // Inherited from RuntimeError
343  virtual void raise() const override { throw *this; }
344  virtual FileParseError* clone() const override {
345  return new FileParseError(*this);
346  }
347 };
348 
349 /*******************************************************************************
350  * Class UserCanceled
351  ******************************************************************************/
352 
376 class UserCanceled final : public Exception {
377 public:
381  UserCanceled() = delete;
382 
386  UserCanceled(const char* file, int line,
387  const QString& msg = QString("User Canceled")) noexcept;
388 
392  UserCanceled(const UserCanceled& other) noexcept;
393 
394  // Inherited from Exception
395  virtual void raise() const override { throw *this; }
396  virtual UserCanceled* clone() const override {
397  return new UserCanceled(*this);
398  }
399 };
400 
401 /*******************************************************************************
402  * End of File
403  ******************************************************************************/
404 
405 } // namespace librepcb
406 
407 #endif // LIBREPCB_EXCEPTIONS_H
virtual RangeError * clone() const override
Definition: exceptions.h:298
RangeError()=delete
Default Constructor.
The LogicError class.
Definition: exceptions.h:182
RangeError(const char *file, int line, const Tval &value, const Tmin &min, const Tmax &max) noexcept
Definition: exceptions.h:283
RuntimeError()=delete
Default Constructor.
virtual UserCanceled * clone() const override
Definition: exceptions.h:396
const QString & getMsg() const
Get the error message (translated)
Definition: exceptions.h:126
Exception()=delete
The default constructor.
virtual LogicError * clone() const override
Definition: exceptions.h:202
The Exception class.
Definition: exceptions.h:87
int mLine
the line number where the exception was thrown
Definition: exceptions.h:163
const QString & getFile() const
Get the source file where the exception was thrown.
Definition: exceptions.h:133
virtual Exception * clone() const override
Definition: exceptions.h:157
QString mMsg
the error message (translated)
Definition: exceptions.h:161
UserCanceled()=delete
Default Constructor.
The UserCanceled class.
Definition: exceptions.h:376
FileParseError()=delete
Default Constructor.
const char * what() const noexceptoverride
reimplemented from std::exception::what()
Definition: exceptions.cpp:49
The RangeError class.
Definition: exceptions.h:260
The RuntimeError class.
Definition: exceptions.h:219
virtual void raise() const override
Definition: exceptions.h:156
This class represents absolute, well-formatted paths to files or directories.
Definition: filepath.h:127
QByteArray mMsgUtf8
the message as an UTF8 byte array
Definition: exceptions.h:166
The FileParseError class.
Definition: exceptions.h:313
virtual RuntimeError * clone() const override
Definition: exceptions.h:244
virtual FileParseError * clone() const override
Definition: exceptions.h:344
virtual ~Exception() noexcept
The destructor.
Definition: exceptions.h:117
virtual ~RuntimeError() noexcept
Destructor.
Definition: exceptions.h:240
QString mFile
the source filename where the exception was thrown
Definition: exceptions.h:162
int getLine() const
Get the line number where the exception was thrown.
Definition: exceptions.h:140
LogicError()=delete
Default Constructor.