Skip to content

Commit

Permalink
Merge pull request #161 from zauguin/fix160
Browse files Browse the repository at this point in the history
Catch exception objects by const reference
  • Loading branch information
zauguin authored May 20, 2018
2 parents b6a1321 + ac0db3e commit 12ead5c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ int main() {
db << "select count(*) from user" >> str_count;
cout << "scount : " << str_count << endl;
}
catch (exception& e) {
catch (const exception& e) {
cout << e.what() << endl;
}
}
Expand Down Expand Up @@ -286,7 +286,7 @@ int main() {
// Even more queries ..
}
catch (exception& e) { cout << e.what() << endl; }
catch (const exception& e) { cout << e.what() << endl; }
}
```

Expand Down Expand Up @@ -388,14 +388,14 @@ try {
}
/* if you are trying to catch all sqlite related exceptions
* make sure to catch them by reference */
catch (sqlite_exception& e) {
catch (const sqlite_exception& e) {
cerr << e.get_code() << ": " << e.what() << " during "
<< e.get_sql() << endl;
}
/* you can catch specific exceptions as well,
catch(sqlite::errors::constraint e) { } */
catch(const sqlite::errors::constraint &e) { } */
/* and even more specific exceptions
catch(sqlite::errors::constraint_primarykey e) { } */
catch(const sqlite::errors::constraint_primarykey &e) { } */
```
You can also register a error logging function with `sqlite::error_log`.
Expand All @@ -419,7 +419,7 @@ try {
// inserting again to produce error
db << "insert into person (id, name) values (?,?)" << 1 << "jack";
}
catch (sqlite_exception& e) {}
catch (const sqlite_exception& e) {}
```

Custom SQL functions
Expand Down
12 changes: 6 additions & 6 deletions hdr/sqlite_modern_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,10 @@ namespace sqlite {
if(!ctxt->constructed) new(ctxt) AggregateCtxt<ContextType>();
step<Count, Functions>(db, count, vals, ctxt->obj);
return;
} catch(sqlite_exception &e) {
} catch(const sqlite_exception &e) {
sqlite3_result_error_code(db, e.get_code());
sqlite3_result_error(db, e.what(), -1);
} catch(std::exception &e) {
} catch(const std::exception &e) {
sqlite3_result_error(db, e.what(), -1);
} catch(...) {
sqlite3_result_error(db, "Unknown error", -1);
Expand Down Expand Up @@ -590,10 +590,10 @@ namespace sqlite {
if(!ctxt->constructed) new(ctxt) AggregateCtxt<ContextType>();
store_result_in_db(db,
static_cast<Functions*>(sqlite3_user_data(db))->second(ctxt->obj));
} catch(sqlite_exception &e) {
} catch(const sqlite_exception &e) {
sqlite3_result_error_code(db, e.get_code());
sqlite3_result_error(db, e.what(), -1);
} catch(std::exception &e) {
} catch(const std::exception &e) {
sqlite3_result_error(db, e.what(), -1);
} catch(...) {
sqlite3_result_error(db, "Unknown error", -1);
Expand Down Expand Up @@ -641,10 +641,10 @@ namespace sqlite {
try {
store_result_in_db(db,
(*static_cast<Function*>(sqlite3_user_data(db)))(std::forward<Values>(values)...));
} catch(sqlite_exception &e) {
} catch(const sqlite_exception &e) {
sqlite3_result_error_code(db, e.get_code());
sqlite3_result_error(db, e.what(), -1);
} catch(std::exception &e) {
} catch(const std::exception &e) {
sqlite3_result_error(db, e.what(), -1);
} catch(...) {
sqlite3_result_error(db, "Unknown error", -1);
Expand Down
4 changes: 2 additions & 2 deletions tests/error_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ TEST_CASE("error_log works", "[log]") {
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
// triger primarykey constraint of 'id'
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "bob";
} catch (errors::constraint& e) { }
} catch (const errors::constraint& e) { }
REQUIRE(track.primarykey_called == true);
REQUIRE(track.constraint_called == false);
track.primarykey_called = false;
Expand All @@ -54,7 +54,7 @@ TEST_CASE("error_log works", "[log]") {
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
// trigger unique constraint of 'name'
db << "INSERT INTO person (id,name) VALUES (?,?)" << 2 << "jack";
} catch (errors::constraint& e) { }
} catch (const errors::constraint& e) { }

REQUIRE(track.primarykey_called == false);
REQUIRE(track.constraint_called == true);
Expand Down
2 changes: 1 addition & 1 deletion tests/mov_ctor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ TEST_CASE("database lifecycle", "move_ctor") {

bool failed = false;
try { dbFront dbf; }
catch(sqlite_exception& e) { failed = true; }
catch(const sqlite_exception& e) { failed = true; }
catch(...) { failed = true; }

REQUIRE(failed == false);
Expand Down
2 changes: 1 addition & 1 deletion tests/sqlcipher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ TEST_CASE("sqlcipher works", "[sqlcipher]") {
config.key = "DebugKey2";
sqlcipher_database db(file.fname, config);
db << "INSERT INTO foo VALUES (?, ?)" << 3 << "fail";
} catch(errors::notadb) {
} catch(const errors::notadb&) {
failed = true;
// Expected, wrong key
}
Expand Down

0 comments on commit 12ead5c

Please sign in to comment.