PDA

View Full Version : How to add simple custom exception handler?



arcull
15th July 2014, 19:13
Hi everyone. I would like to create a custom class for known exception (extending QtConcurrent::Exception), but unfortunately I'm missing a few puzzles... Here is what I have s far illustrating what I would like to achieve.
class myException: public QtConcurrent::Exception {
public:
myException(const char *desc) {
QMessageBox::critical(NULL,QObject::tr("Warning"),QObject::tr(desc));
}

~myException() throw();

}; in a function I try to raise exception like this:
int b = 0;
if (b == 0)
myException *exc = new myException("division with 0!"); but can't make it compile, I get:
error: undefined reference to `vtable for myException'. Can you please correct this sample so that it works, much thanks.

anda_skoa
15th July 2014, 20:39
You declare a destructor for myException but you are not defining it (missing body).

Also using a message box in the excepion's constructor does not make sense, especially since this is concurrent exception.

Cheers,
_

arcull
15th July 2014, 21:36
You declare a destructor for myException but you are not defining it (missing body). ok I probably don't need a destructor in my case
Also using a message box in the excepion's constructor does not make sense, especially since this is concurrent exception. well it may be a bad idea, however I would like to show a message with description of what went wrong to the user, should I do it some other way? Please provide a short sample code.. I've changed my sample like this:
class myException: public QtConcurrent::Exception {
public:
myException(const char *desc) {
//QMessageBox::critical(NULL,QObject::tr("Warning"),QObject::tr(desc));
qDebug() << desc;
}
}; and the calling function
myException exc("division with 0");
throw exc; Which works so so..., if I leave the messagebox, this gets displayed but soon afterwards the whole app crashes with something like "Microsoft Visual C++ Runtime Library: This application has requested the Runtime to terminate in an unusual way." If I use qDebug instead, the message is displayed only in "application output window", but still crashes the same way. I guesss I'm doing something wrong here, please show the right direction, a snippet of code would be useful, thanks.

anda_skoa
16th July 2014, 08:38
however I would like to show a message with description of what went wrong to the user, should I do it some other way?

Handling an exception is what the catch block is for.

How does your's look?

Cheers,
_

arcull
16th July 2014, 16:12
I was trying to handle exceptions in few functions and forgot to add a try-catch block in one of them, that's why my app crashed with "Microsoft Visual C++ Runtime Library:blah blah".My working sample now looks like this:
class myException: public QtConcurrent::Exception {
public:
myException(const char *desc) {
excdesc = desc;
}
const char *excdesc;
}; and a part of one function
try {
//some other code...
int b = 0;
if (b == 0) {
myException exc("you can not divide with 0");
throw exc;
}
} catch (myException & e) {
QMessageBox::critical(this,tr("Warning"),tr(e.excdesc));
}
which works ok, however I see little point have an extra class myException for as little as showing a simple error description. Couldn't I do just something like this:
try {
//some other code...
int b = 0;
if (b == 0) {
throw "you can not divide with 0";
}
} catch (char *e) {
QMessageBox::critical(this,tr("Warning"),tr(e));
} but my app crashes this way. Please suggest, thanks.

anda_skoa
16th July 2014, 19:07
QtConcurrent::Exception is for throwing expections in a threadpool worker thread but catching it in the calling thread (usually the main thread).

If all you need is a thread local exception, just use std::exception http://www.cplusplus.com/reference/exception/exception/

Cheers,
_

arcull
17th July 2014, 19:14
QtConcurrent::Exception is for throwing expections in a threadpool worker thread but catching it in the calling thread (usually the main thread).

If all you need is a thread local exception, just use std::exception http://www.cplusplus.com/reference/exception/exception/

Cheers,
_ Thanks I agree, but currently I just don't see how to pass a custom error message to the user this way.
QMessageBox::critical(this,tr("Warning"),tr(e.what())); but how do I put custom string to "what" when throwing exceptions. Thanks again.

stampede
17th July 2014, 20:10
how do I put custom string to "what" when throwing exceptions
via constructor


void fn(){
throw std::runtime_error("this is my message");
}

or

class my_exception : public std::exception{
public:
my_exception(const QString& string){
_data = string.toLocal8it();
}
const char * what() const noexcept{
return _data.constData();
}
private:
QByteArray _data;
};
...
void fn(){
throw my_exception(QObject::tr("this is my localized message!"));
}

Infinity
17th July 2014, 20:11
Simply reimplement the virtual what method of your exception subclass.
You can also extend the constructor to take an error message, store it and return it in the what reimplementation (like in std::logic_error).

arcull
17th July 2014, 20:36
Thanks both, ok I'll have to create my custom class extending std::exception. Good, case solved :)