BPS error exception class. More...
#include <bpsexception.h>
Public Member Functions | |
| BpsException & | addLocation (const QString &aFilename, quint32 aLinenumber, const QString &aFunction) |
| Add another location to an existing exception. | |
| BpsException & | appendMessage (const QString &aText) |
| Append text to the error message of the exception. | |
| BpsException () | |
| Default constructor for the BpsException objects. | |
| BpsException (const BpsException &aException) | |
| Copy constructor for the BpsException objects. | |
| BpsException (const QString &aFilename, quint32 aLinenumber, const QString &aFunction, const QString &aMessage) | |
| Standard constructor for the BpsException objects. | |
| BpsException (const QScriptValue &aException, const QString &aFilename=QString()) | |
| Constructor from QtScript exceptions. | |
| QString | message () const |
| Get message. | |
| BpsException & | setMessage (const QString &aMessage) |
| Set the error message for the exception. | |
| QString | text () const |
| Get the message, along with a log of the locations stack. | |
| QScriptValue | throwScriptError (QScriptContext *aContext) const |
| Throw a script error of the exception. | |
BPS error exception class.
In the BPS environment error handling is heavily using exceptions (in contrast to the underlying Qt framework, where exceptions are for historical reasons not used in c++ at all). The base class of all exceptions thrown by the BPS c++ library is BpsException.
The class holds an error message, plus an arbitrary number of locations made up each by the source file name, the source file line number, and the function or method name. The first location is the place where the exception was thrown while the following locations are traceback informations collected by catching, tracing and rethrowing the exception.
BpsExceptions integrate seamless with script exceptions, so that exceptions thrown by c++ can be catched as Error objects by script code, and vice versa.
| BpsException::BpsException | ( | const BpsException & | aException | ) |
Copy constructor for the BpsException objects.
| [in] | aException | Reference of the BpsException object to copy. |
| BpsException::BpsException | ( | const QString & | aFilename, |
| quint32 | aLinenumber, | ||
| const QString & | aFunction, | ||
| const QString & | aMessage | ||
| ) |
Standard constructor for the BpsException objects.
This is the most used constructor variant in applications. Typical usage in throwing exceptions as:
throw BpsException( __FILE__, __LINE__, __FUNCTION__, tr("Some error message about %1.").arg(topic));
For better convenience use the bpsMakeException macro:
throw bpsMakeException(tr("Some error message about %1.").arg(topic));
| [in] | aFilename | Source file name, most often provided by __FILE__ macro. |
| [in] | aLinenumber | Source file line number, most often provided by __LINE__ macro. |
| [in] | aFunction | Function name, most often provided by __FUNCTION__ macro. |
| [in] | aMessage | The error message. |
| BpsException::BpsException | ( | const QScriptValue & | aException, |
| const QString & | aFilename = QString() |
||
| ) |
Constructor from QtScript exceptions.
| [in] | aException | Reference of the exception to copy. |
| [in] | aFilename | Source file name if known. Will be used if script value has no file info. |
| BpsException& BpsException::addLocation | ( | const QString & | aFilename, |
| quint32 | aLinenumber, | ||
| const QString & | aFunction | ||
| ) |
Add another location to an existing exception.
Typically used in catch blocks:
try { // doing some things here that might throw BpsException. } catch (BpsException& exc) { throw exc.addLocation(__FILE__, __LINE__, __FUNCTION__); }
For better convenience use the macro bpsTraceException instead:
try { // doing some things here that might throw BpsException. } catch (BpsException& exc) { throw bpsTraceException(exc); }
| [in] | aFilename | Source file name, most often provided by __FILE__ macro. |
| [in] | aLinenumber | Source file line number, most often provided by __LINE__ macro. |
| [in] | aFunction | Function name, most often provided by __FUNCTION__ macro. |
| BpsException& BpsException::appendMessage | ( | const QString & | aText | ) |
Append text to the error message of the exception.
| [in] | aText | The text to append to the message. |
| QString BpsException::message | ( | ) | const |
Get message.
| BpsException& BpsException::setMessage | ( | const QString & | aMessage | ) |
Set the error message for the exception.
| [in] | aMessage | The new error message. |
| QString BpsException::text | ( | ) | const |
Get the message, along with a log of the locations stack.
try { //... try { //... if (somethingwrong) throw bpsNewException("Something is wrong."); //... } catch (BpsException& exc) { throw bpsAddLocation(exc); } //... } catch (const BpsException& exc) { QMessageBox::critical(this, "Error", exc.text()); // exc.text() = // "Something is wrong.\n" // "---\n" // "myfile.cpp[45] MyClass::myfunc()\n" // "myfile.cpp[49] MyClass::myfunc()\n"; }
| QScriptValue BpsException::throwScriptError | ( | QScriptContext * | aContext | ) | const |
Throw a script error of the exception.
This is useful in c++ code that was called from a script, for example in classes derived from QScriptable.
QScriptValue myScriptableFunction(QScriptContext* aContext, QScriptEngine* aEngine)
{
try {
// Some code here that might throw a BpsException
return result;
}
catch (const BpsException& exc) {
return exc.throwScriptError(aContext);
}
}
| [in] | aContext | Pointer to the script context. |