mokiliii Lo
mokiliii Lo

Reputation: 637

Beginner C++ - Trying to catch an exception

So basically I have this code:

#include <iostream>
using namespace std;

#include "libsqlite.hpp"

int main()
{
    sqlite::sqlite db( "firefly.sqlite" );

    auto cur = db.get_statement(); 

    // cur->set_sql( "CREATE TABLE students_mark (sid INT, name VARCHAR(255), pt1_mark INT, pt2_mark INT, cw_mark INT, PRIMARY KEY(sid, name));" );
    cur->set_sql( "INSERT INTO students_mark (sid, name, pt1_mark, pt2_mark, cw_mark) VALUES (?, ?, ?, ?, ?);" );

    cur->prepare();
}

Which gives me this error:

libc++abi.dylib: terminating with uncaught exception of type sqlite::exception: std::exception
Abort trap: 6

So I tried to catch the exception to understand it better, but I can't seem to archieve this goal.

Here's what I did:

try {
    cur->prepare();
} catch(exception& e) {
    cout << "Error: " << e.what() << endl;
}

and this gives me this output: Error: std::exception

What can I do?

Thanks a lot

Upvotes: 1

Views: 1176

Answers (1)

Martijn Courteaux
Martijn Courteaux

Reputation: 68857

Exceptions exist for several reasons. But right now it's there to let you know somethings wrong. Simply catching it won't solve the issue itself. It will only change the behaviour of your program after the problem occurred.

You caught the exception just fine using catch (sqlite::exception &e), now you know what is the issue. Now, research and fix what the exception is telling you.

Upvotes: 1

Related Questions