Skip to content

Commit ac0db3e

Browse files
committed
Catch exception objects by const reference
A version of SqliteModernCpp#160 on the `dev` branch.
1 parent b6a1321 commit ac0db3e

File tree

5 files changed

+16
-16
lines changed

5 files changed

+16
-16
lines changed

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ int main() {
8383
db << "select count(*) from user" >> str_count;
8484
cout << "scount : " << str_count << endl;
8585
}
86-
catch (exception& e) {
86+
catch (const exception& e) {
8787
cout << e.what() << endl;
8888
}
8989
}
@@ -286,7 +286,7 @@ int main() {
286286
287287
// Even more queries ..
288288
}
289-
catch (exception& e) { cout << e.what() << endl; }
289+
catch (const exception& e) { cout << e.what() << endl; }
290290
}
291291
```
292292

@@ -388,14 +388,14 @@ try {
388388
}
389389
/* if you are trying to catch all sqlite related exceptions
390390
* make sure to catch them by reference */
391-
catch (sqlite_exception& e) {
391+
catch (const sqlite_exception& e) {
392392
cerr << e.get_code() << ": " << e.what() << " during "
393393
<< e.get_sql() << endl;
394394
}
395395
/* you can catch specific exceptions as well,
396-
catch(sqlite::errors::constraint e) { } */
396+
catch(const sqlite::errors::constraint &e) { } */
397397
/* and even more specific exceptions
398-
catch(sqlite::errors::constraint_primarykey e) { } */
398+
catch(const sqlite::errors::constraint_primarykey &e) { } */
399399
```
400400
401401
You can also register a error logging function with `sqlite::error_log`.
@@ -419,7 +419,7 @@ try {
419419
// inserting again to produce error
420420
db << "insert into person (id, name) values (?,?)" << 1 << "jack";
421421
}
422-
catch (sqlite_exception& e) {}
422+
catch (const sqlite_exception& e) {}
423423
```
424424

425425
Custom SQL functions

hdr/sqlite_modern_cpp.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,10 @@ namespace sqlite {
526526
if(!ctxt->constructed) new(ctxt) AggregateCtxt<ContextType>();
527527
step<Count, Functions>(db, count, vals, ctxt->obj);
528528
return;
529-
} catch(sqlite_exception &e) {
529+
} catch(const sqlite_exception &e) {
530530
sqlite3_result_error_code(db, e.get_code());
531531
sqlite3_result_error(db, e.what(), -1);
532-
} catch(std::exception &e) {
532+
} catch(const std::exception &e) {
533533
sqlite3_result_error(db, e.what(), -1);
534534
} catch(...) {
535535
sqlite3_result_error(db, "Unknown error", -1);
@@ -590,10 +590,10 @@ namespace sqlite {
590590
if(!ctxt->constructed) new(ctxt) AggregateCtxt<ContextType>();
591591
store_result_in_db(db,
592592
static_cast<Functions*>(sqlite3_user_data(db))->second(ctxt->obj));
593-
} catch(sqlite_exception &e) {
593+
} catch(const sqlite_exception &e) {
594594
sqlite3_result_error_code(db, e.get_code());
595595
sqlite3_result_error(db, e.what(), -1);
596-
} catch(std::exception &e) {
596+
} catch(const std::exception &e) {
597597
sqlite3_result_error(db, e.what(), -1);
598598
} catch(...) {
599599
sqlite3_result_error(db, "Unknown error", -1);
@@ -641,10 +641,10 @@ namespace sqlite {
641641
try {
642642
store_result_in_db(db,
643643
(*static_cast<Function*>(sqlite3_user_data(db)))(std::forward<Values>(values)...));
644-
} catch(sqlite_exception &e) {
644+
} catch(const sqlite_exception &e) {
645645
sqlite3_result_error_code(db, e.get_code());
646646
sqlite3_result_error(db, e.what(), -1);
647-
} catch(std::exception &e) {
647+
} catch(const std::exception &e) {
648648
sqlite3_result_error(db, e.what(), -1);
649649
} catch(...) {
650650
sqlite3_result_error(db, "Unknown error", -1);

tests/error_log.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ TEST_CASE("error_log works", "[log]") {
4343
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
4444
// triger primarykey constraint of 'id'
4545
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "bob";
46-
} catch (errors::constraint& e) { }
46+
} catch (const errors::constraint& e) { }
4747
REQUIRE(track.primarykey_called == true);
4848
REQUIRE(track.constraint_called == false);
4949
track.primarykey_called = false;
@@ -54,7 +54,7 @@ TEST_CASE("error_log works", "[log]") {
5454
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
5555
// trigger unique constraint of 'name'
5656
db << "INSERT INTO person (id,name) VALUES (?,?)" << 2 << "jack";
57-
} catch (errors::constraint& e) { }
57+
} catch (const errors::constraint& e) { }
5858

5959
REQUIRE(track.primarykey_called == false);
6060
REQUIRE(track.constraint_called == true);

tests/mov_ctor.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ TEST_CASE("database lifecycle", "move_ctor") {
2424

2525
bool failed = false;
2626
try { dbFront dbf; }
27-
catch(sqlite_exception& e) { failed = true; }
27+
catch(const sqlite_exception& e) { failed = true; }
2828
catch(...) { failed = true; }
2929

3030
REQUIRE(failed == false);

tests/sqlcipher.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ TEST_CASE("sqlcipher works", "[sqlcipher]") {
3737
config.key = "DebugKey2";
3838
sqlcipher_database db(file.fname, config);
3939
db << "INSERT INTO foo VALUES (?, ?)" << 3 << "fail";
40-
} catch(errors::notadb) {
40+
} catch(const errors::notadb&) {
4141
failed = true;
4242
// Expected, wrong key
4343
}

0 commit comments

Comments
 (0)