LibrePCB Developers Documentation
Loading...
Searching...
No Matches
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 ******************************************************************************/
31namespace librepcb {
32
33class FilePath;
34
35/*******************************************************************************
36 * Class Exception
37 ******************************************************************************/
38
84class Exception : public QException {
85public:
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
125 const QString& getMsg() const { return mMsg; }
126
132 const QString& getFile() const { return mFile; }
133
139 int getLine() const { return mLine; }
140
152 const char* what() const noexcept override;
153
154 // Inherited from QException (see QException documentation for more details)
155 virtual void raise() const override { throw *this; }
156 virtual Exception* clone() const override { return new Exception(*this); }
157
158private:
159 // Attributes
160 QString mMsg;
161 QString mFile;
162 int mLine;
163
164 // Cached Attributes
165 mutable QByteArray mMsgUtf8;
166};
167
168/*******************************************************************************
169 * Class LogicError
170 ******************************************************************************/
171
181class LogicError final : public Exception {
182public:
186 LogicError() = delete;
187
191 LogicError(const char* file, int line,
192 const QString& msg = QString("Logic Error")) noexcept;
193
197 LogicError(const LogicError& other) noexcept;
198
199 // Inherited from Exception
200 virtual void raise() const override { throw *this; }
201 virtual LogicError* clone() const override { return new LogicError(*this); }
202};
203
204/*******************************************************************************
205 * Class RuntimeError
206 ******************************************************************************/
207
218class RuntimeError : public Exception {
219public:
223 RuntimeError() = delete;
224
228 RuntimeError(const char* file, int line,
229 const QString& msg = QString("Runtime Error")) noexcept;
230
234 RuntimeError(const RuntimeError& other) noexcept;
235
239 virtual ~RuntimeError() noexcept {}
240
241 // Inherited from Exception
242 virtual void raise() const override { throw *this; }
243 virtual RuntimeError* clone() const override {
244 return new RuntimeError(*this);
245 }
246};
247
248/*******************************************************************************
249 * Class RangeError
250 ******************************************************************************/
251
259class RangeError final : public RuntimeError {
260public:
264 RangeError() = delete;
265
269 RangeError(const char* file, int line,
270 const QString& msg = QString("Range Error")) noexcept;
271
281 template <typename Tval, typename Tmin, typename Tmax>
282 RangeError(const char* file, int line, const Tval& value, const Tmin& min,
283 const Tmax& max) noexcept
284 : RangeError(file, line,
285 QString("Range error: %1 not in [%2..%3]")
286 .arg(value)
287 .arg(min)
288 .arg(max)) {}
289
293 RangeError(const RangeError& other) noexcept;
294
295 // Inherited from RuntimeError
296 virtual void raise() const override { throw *this; }
297 virtual RangeError* clone() const override { return new RangeError(*this); }
298};
299
300/*******************************************************************************
301 * Class FileParseError
302 ******************************************************************************/
303
312class FileParseError final : public RuntimeError {
313public:
317 FileParseError() = delete;
318
328 FileParseError(const char* file, int line, const FilePath& filePath,
329 const QString& invalidFileContent = QString(),
330 const QString& msg = QString("File Parse Error")) noexcept;
331
335 FileParseError(const FileParseError& other) noexcept;
336
337 // Inherited from RuntimeError
338 virtual void raise() const override { throw *this; }
339 virtual FileParseError* clone() const override {
340 return new FileParseError(*this);
341 }
342};
343
344/*******************************************************************************
345 * Class UserCanceled
346 ******************************************************************************/
347
371class UserCanceled final : public Exception {
372public:
376 UserCanceled() = delete;
377
381 UserCanceled(const char* file, int line,
382 const QString& msg = QString("User Canceled")) noexcept;
383
387 UserCanceled(const UserCanceled& other) noexcept;
388
389 // Inherited from Exception
390 virtual void raise() const override { throw *this; }
391 virtual UserCanceled* clone() const override {
392 return new UserCanceled(*this);
393 }
394};
395
396/*******************************************************************************
397 * End of File
398 ******************************************************************************/
399
400} // namespace librepcb
401
402#endif
The Exception class.
Definition exceptions.h:84
QString mFile
the source filename where the exception was thrown
Definition exceptions.h:161
Exception()=delete
The default constructor.
const QString & getFile() const
Get the source file where the exception was thrown.
Definition exceptions.h:132
int mLine
the line number where the exception was thrown
Definition exceptions.h:162
virtual void raise() const override
Definition exceptions.h:155
QString mMsg
the error message (translated)
Definition exceptions.h:160
QByteArray mMsgUtf8
the message as an UTF8 byte array
Definition exceptions.h:165
const QString & getMsg() const
Get the error message (translated)
Definition exceptions.h:125
int getLine() const
Get the line number where the exception was thrown.
Definition exceptions.h:139
const char * what() const noexcept override
reimplemented from std::exception::what()
Definition exceptions.cpp:49
virtual Exception * clone() const override
Definition exceptions.h:156
The FileParseError class.
Definition exceptions.h:312
virtual void raise() const override
Definition exceptions.h:338
virtual FileParseError * clone() const override
Definition exceptions.h:339
FileParseError()=delete
Default Constructor.
This class represents absolute, well-formatted paths to files or directories.
Definition filepath.h:127
The LogicError class.
Definition exceptions.h:181
virtual LogicError * clone() const override
Definition exceptions.h:201
virtual void raise() const override
Definition exceptions.h:200
LogicError()=delete
Default Constructor.
The RangeError class.
Definition exceptions.h:259
virtual void raise() const override
Definition exceptions.h:296
virtual RangeError * clone() const override
Definition exceptions.h:297
RangeError()=delete
Default Constructor.
The RuntimeError class.
Definition exceptions.h:218
RuntimeError()=delete
Default Constructor.
virtual void raise() const override
Definition exceptions.h:242
virtual RuntimeError * clone() const override
Definition exceptions.h:243
The UserCanceled class.
Definition exceptions.h:371
virtual void raise() const override
Definition exceptions.h:390
virtual UserCanceled * clone() const override
Definition exceptions.h:391
UserCanceled()=delete
Default Constructor.
Definition occmodel.cpp:76