PDA

View Full Version : How to try-catch exception in external library?



arcull
13th July 2014, 11:43
Hi, everyone. I'm developing a desktop gui app, which is using an external library FANN (with source code). The problem is that I don't know how to catch exceptions which happen in that library. Whatever goes wrong when I invoke external functions crashes my app. So far I tried something like this:
bool MainWindow::RunFann() {
try {
QDebugStream qds (std::cout,ui->textEdit);

if ((ui->lineEdit_4->text() == "") || (ui->lineEdit_5->text() == "")) {
QMessageBox::warning(this,tr("Warning"),tr("Select a valid test data file and a trainded FANN file first!"));
return(false);
}

RedirectOutput(0);

QByteArray ba = ui->lineEdit_5->text().toLocal8Bit();
struct fann *ann = fann_create_from_file(ba.data());

ba = ui->lineEdit_4->text().toLocal8Bit();
struct fann_train_data *TestData = fann_read_train_from_file(ba.data()); //< external lib function call

fann_type *result;
printf("SEQUENCE : SOURCE : CALCULATED\n");
for (int i=0;i<TestData->num_data;i++) {
result = fann_run(ann,TestData->input[i]); //< external lib function call
for (int j=0;j<ann->num_output;j++) {
char str[10];
sprintf(str,"%d %s %f %s %f\n",i,":",result[j],":",TestData->output[i][j]);
printf(str);
}
}
RedirectOutput(1);
} catch (std::exception & e) {
printf("something weired happened..");
}

} Any help much appreciated.

stampede
13th July 2014, 13:55
On which line does it crash ? What is the stack trace ? Have you tried to verify that "TestData" and "ann" pointers are not null ?

anda_skoa
13th July 2014, 18:38
And are you sure that str is long enough to hold both the formatted integer and floating point number?
Maybe be safe and use QString::sprintf() instead?

Also this looks a lot like a C API, not a C++ API with exceptions.

Cheers,
_

arcull
13th July 2014, 20:18
On which line does it crash ? What is the stack trace ? Have you tried to verify that "TestData" and "ann" pointers are not null ? it does crash when invoking "fann_read_train_from_file". The point is that I have intentionaly made it crash, just to test if I can handle the exceptions. "fann_read_train_from_file" requires a well formated file and I selected a random binary file as input. With a proper file it works ok, but the point here is, to make my app able to handle the FANN library exceptions.
And are you sure that str is long enough to hold both the formatted integer and floating point number?
Maybe be safe and use QString::sprintf() instead?

Also this looks a lot like a C API, not a C++ API with exceptions.I'm not sure about that, I'm still learning C an Qt :( I think it is a C API http://leenissen.dk/fann/wp/download/, but if its not, can I still handle excpetions when invoking it's functions? Sorry if this looks an elementry issue, but I am a newbie..

anda_skoa
14th July 2014, 07:42
I think it is a C API http://leenissen.dk/fann/wp/download/, but if its not, can I still handle excpetions when invoking it's functions? Sorry if this looks an elementry issue, but I am a newbie..

A C library does not use exceptions, that is a C++ concept, i.e. there is nothing to catch if this library is written in C.

My guess is that what you are seeing and what lead to this misunderstanding is a "runtime exception" on Windows, i.e. a segmentation fault on Unix.
You'll need to find the cause and fix it at the point where it happens or make sure that the execution path does not reach that location and state.

Cheers,
_

arcull
14th July 2014, 08:39
A C library does not use exceptions, that is a C++ concept, i.e. there is nothing to catch if this library is written in C. Ok, I got it now, thanks for making that clear.