LibrePCB Developers Documentation
Exception Class Reference

The Exception class. More...

#include <exceptions.h>

+ Inheritance diagram for Exception:
+ Collaboration diagram for Exception:

Public Member Functions

 Exception ()=delete
 The default constructor. More...
 
 Exception (const Exception &other) noexcept
 The copy constructor (needed for clone()) More...
 
 Exception (const char *file, int line, const QString &msg=QString("Exception")) noexcept
 The constructor which is used to throw an exception. More...
 
virtual ~Exception () noexcept
 The destructor. More...
 
const QString & getMsg () const
 Get the error message (translated) More...
 
const QString & getFile () const
 Get the source file where the exception was thrown. More...
 
int getLine () const
 Get the line number where the exception was thrown. More...
 
const char * what () const noexcept override
 reimplemented from std::exception::what() More...
 
virtual void raise () const override
 
virtual Exceptionclone () const override
 

Private Attributes

QString mMsg
 the error message (translated) More...
 
QString mFile
 the source filename where the exception was thrown More...
 
int mLine
 the line number where the exception was thrown More...
 
QByteArray mMsgUtf8
 the message as an UTF8 byte array More...
 

Detailed Description

The Exception class.

This is an exception base class. It inherits from the QException class, which implements an exception class which can be transferred across threads. QException inherits from std::exception. There are several subclasses of the class Exception, see inheritance diagram.

Note
Exceptions must always be thrown by value and caught by (const) reference!

For more information about the QException class read the Qt documentation.

Each Exception object holds following attributes:

  • a message (mMsg): error message in the user's language (can be printed directly to a QMessageBox or similar)
  • the filename of the source file where the exception was thrown (mFile)
  • the line number where the exception was thrown (mLine)
Note
Every exception will automatically print a debug message (see librepcb::Debug) of type librepcb::Debug::DebugLevel_t::Exception.

Example how to use exceptions:

void foo(int i) {
if (i < 0) {
throw Exception(__FILE__, __LINE__, tr("Invalid argument: %1").arg(i));
}
}
void bar() noexcept {
try {
foo(-5);
} catch (const Exception& e) {
QMessageBox::critical(0, tr("Error"), e.getMsg());
}
}
Exception()=delete
The default constructor.
Warning
Please read the "Exception Safety" notes from the Qt Project documentation before writing source code which throws exceptions, there are some important things to know: http://qt-project.org/doc/qt-5/exceptionsafety.html

Constructor & Destructor Documentation

◆ Exception() [1/3]

Exception ( )
delete

The default constructor.

+ Here is the caller graph for this function:

◆ Exception() [2/3]

Exception ( const Exception other)
noexcept

The copy constructor (needed for clone())

◆ Exception() [3/3]

Exception ( const char *  file,
int  line,
const QString &  msg = QString("Exception") 
)
noexcept

The constructor which is used to throw an exception.

Parameters
fileThe source file where the exception was thrown (use FILE)
lineThe line number where the exception was thrown (use LINE)
msgAn error message in the user's language (use QObject::tr()). This message can be used in message boxes.
+ Here is the call graph for this function:

◆ ~Exception()

virtual ~Exception ( )
inlinevirtualnoexcept

The destructor.

Member Function Documentation

◆ getMsg()

const QString & getMsg ( ) const
inline

Get the error message (translated)

Returns
The error message in the user's language

◆ getFile()

const QString & getFile ( ) const
inline

Get the source file where the exception was thrown.

Returns
The filename

◆ getLine()

int getLine ( ) const
inline

Get the line number where the exception was thrown.

Returns
The line number

◆ what()

const char * what ( ) const
overridenoexcept

reimplemented from std::exception::what()

Warning
This method is only for compatibility reasons with the base class std::exception. Normally, you should not use this method. Use getMsg() instead.
Note
The returned pointer is valid as long as the exception object exists
Returns
the error message as a C-string (const char*) in the local encoding
+ Here is the caller graph for this function:

◆ raise()

virtual void raise ( ) const
inlineoverridevirtual

◆ clone()

virtual Exception * clone ( ) const
inlineoverridevirtual

Reimplemented in LogicError, RuntimeError, RangeError, FileParseError, and UserCanceled.

+ Here is the call graph for this function:

Member Data Documentation

◆ mMsg

QString mMsg
private

the error message (translated)

◆ mFile

QString mFile
private

the source filename where the exception was thrown

◆ mLine

int mLine
private

the line number where the exception was thrown

◆ mMsgUtf8

QByteArray mMsgUtf8
mutableprivate

the message as an UTF8 byte array


The documentation for this class was generated from the following files: