Results 1 to 6 of 6

Thread: How to try-catch exception in external library?

  1. #1
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to try-catch exception in external library?

    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:
    Qt Code:
    1. bool MainWindow::RunFann() {
    2. try {
    3. QDebugStream qds (std::cout,ui->textEdit);
    4.  
    5. if ((ui->lineEdit_4->text() == "") || (ui->lineEdit_5->text() == "")) {
    6. QMessageBox::warning(this,tr("Warning"),tr("Select a valid test data file and a trainded FANN file first!"));
    7. return(false);
    8. }
    9.  
    10. RedirectOutput(0);
    11.  
    12. QByteArray ba = ui->lineEdit_5->text().toLocal8Bit();
    13. struct fann *ann = fann_create_from_file(ba.data());
    14.  
    15. ba = ui->lineEdit_4->text().toLocal8Bit();
    16. struct fann_train_data *TestData = fann_read_train_from_file(ba.data()); //< external lib function call
    17.  
    18. fann_type *result;
    19. printf("SEQUENCE : SOURCE : CALCULATED\n");
    20. for (int i=0;i<TestData->num_data;i++) {
    21. result = fann_run(ann,TestData->input[i]); //< external lib function call
    22. for (int j=0;j<ann->num_output;j++) {
    23. char str[10];
    24. sprintf(str,"%d %s %f %s %f\n",i,":",result[j],":",TestData->output[i][j]);
    25. printf(str);
    26. }
    27. }
    28. RedirectOutput(1);
    29. } catch (std::exception & e) {
    30. printf("something weired happened..");
    31. }
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 
    Any help much appreciated.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to try-catch exception in external library?

    On which line does it crash ? What is the stack trace ? Have you tried to verify that "TestData" and "ann" pointers are not null ?

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to try-catch exception in external library?

    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,
    _

  4. #4
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to try-catch exception in external library?

    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..

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to try-catch exception in external library?

    Quote Originally Posted by arcull View Post
    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,
    _

  6. #6
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to try-catch exception in external library?

    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.

Similar Threads

  1. Replies: 5
    Last Post: 4th October 2017, 12:32
  2. Replies: 3
    Last Post: 24th April 2012, 00:06
  3. Exception handling in external library - good or bad idea
    By kornicameister in forum Qt Programming
    Replies: 0
    Last Post: 20th February 2012, 14:25
  4. Using external ANSI C library
    By npascual in forum Qt Programming
    Replies: 2
    Last Post: 5th May 2011, 12:02
  5. Adding an external Library
    By afflictedd2 in forum Qt Programming
    Replies: 1
    Last Post: 13th December 2008, 05:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.