1

We have a QT based C++ application. In which we are using third party DLL files too. But, C++ try and catch does not work at all.

For example:

#include <QCoreApplication>
#include <QDebug>
#include <QException>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    int arr[10];
    try
    {
        arr[11] = 30;
    }
    catch (const std::out_of_range& e)
    {
        qDebug() << "Exception out of range occurred ..." << e.what();
    }
    catch (...)
    {
        qDebug() << "Unknown Exception occurred...";
    }

    return a.exec();
}

Above is the minimal example. In the above, it crashes the program. Is there a way to handle this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hetal agrawal
  • 53
  • 1
  • 1
  • 8

3 Answers3

2

Reading or writing out of array bounds is undefined behavior. There's no guarantee that it will crash, throw an exception, or do anything at all. It's just malformed code.

If you want an array-like container with bounds checking there are std::array, std::vector, and perhaps std::deque in the standard library. They all have an at() member function that does bounds checking and will throw a std::out_of_range exception.

Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
  • In this case, writing a std::set_terminate, will help? – hetal agrawal Feb 25 '19 at 11:41
  • 1
    @hetalagrawal Setting a terminate handler is useful to handle the case when exception handling fails (such as an uncaught `std::out_of_range`). Remember that out-of-bounds on a plain array is undefined behavior so `std::set_terminate` isn't the solution for the code in your question. – Blastfurnace Feb 25 '19 at 13:36
  • Why we can not handle null pointer access using try, catch block in c++. Is the reason is same as it leads to undefined behavior? – hetal agrawal Feb 28 '19 at 13:18
  • 1
    @hetalagrawal A segfault caused by a null pointer access isn't a C++ exception. See: [Is it possible to catch a segfault with try/catch?](https://stackoverflow.com/questions/40144571/is-it-possible-to-catch-a-segfault-with-try-catch) – Blastfurnace Feb 28 '19 at 16:52
1

std::out_of_range

It may be thrown by the member functions of std::bitset and std::basic_string, by std::stoi and std::stod families of functions, and by the bounds-checked member access functions (e.g. std::vector::at and std::map::at)

Your try block has neither of these things. Out of bounds access to plain C style arrays is undefined behaviour. This is often manifested as crashes, as you have experienced.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
0

To answer your question:

"C++ try catch does not work for third party library"

No! C++ try catch does work with a third-party (Qt) library, as shown in the example below.

But the code that you are showing is not an MCVE. Therefore it is difficult to say, what exactly is causing the problem that you say you are having.

#include <QCoreApplication>
#include <QDebug>
#include <QException>

class testException : public QException
{
public:
    testException(QString const& message) :
        message(message)
    {}

    virtual ~testException()
    {}

    void raise() const { throw *this; }
    testException *clone() const { return new testException(*this); }

    QString getMessage() const {
        return message;
    }
private:
    QString message;
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    try
    {
        // throw std::out_of_range("blah");
        throw testException("blah");
    }
    catch (const std::out_of_range& e)
    {
        qDebug() << "Exception out of range occurred ...";
    }
    catch (...)
    {
        qDebug() << "Unknown Exception occured...";
    }

    return a.exec();
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Duck Dodgers
  • 3,409
  • 8
  • 29
  • 43