From ac0db3e67ba17ff816dbd31ea80cdf1ff871f37a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20Kr=C3=BCger?= Date: Tue, 3 Apr 2018 11:08:53 +0200 Subject: [PATCH] Catch exception objects by const reference A version of #160 on the `dev` branch. --- README.md | 12 ++++++------ hdr/sqlite_modern_cpp.h | 12 ++++++------ tests/error_log.cc | 4 ++-- tests/mov_ctor.cc | 2 +- tests/sqlcipher.cc | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index b7836afb..4d85af2f 100644 --- a/README.md +++ b/README.md @@ -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; } } @@ -286,7 +286,7 @@ int main() { // Even more queries .. } - catch (exception& e) { cout << e.what() << endl; } + catch (const exception& e) { cout << e.what() << endl; } } ``` @@ -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`. @@ -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 diff --git a/hdr/sqlite_modern_cpp.h b/hdr/sqlite_modern_cpp.h index 69d63476..964f993c 100644 --- a/hdr/sqlite_modern_cpp.h +++ b/hdr/sqlite_modern_cpp.h @@ -526,10 +526,10 @@ namespace sqlite { if(!ctxt->constructed) new(ctxt) AggregateCtxt(); step(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); @@ -590,10 +590,10 @@ namespace sqlite { if(!ctxt->constructed) new(ctxt) AggregateCtxt(); store_result_in_db(db, static_cast(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); @@ -641,10 +641,10 @@ namespace sqlite { try { store_result_in_db(db, (*static_cast(sqlite3_user_data(db)))(std::forward(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); diff --git a/tests/error_log.cc b/tests/error_log.cc index 237a46f0..63e9e9f4 100644 --- a/tests/error_log.cc +++ b/tests/error_log.cc @@ -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; @@ -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); diff --git a/tests/mov_ctor.cc b/tests/mov_ctor.cc index 8a70d430..048fd05d 100644 --- a/tests/mov_ctor.cc +++ b/tests/mov_ctor.cc @@ -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); diff --git a/tests/sqlcipher.cc b/tests/sqlcipher.cc index 4c91894e..2fc2f10c 100644 --- a/tests/sqlcipher.cc +++ b/tests/sqlcipher.cc @@ -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 }