Skip to content

Commit a22241e

Browse files
committed
Add documentation and change error codes to int
1 parent de9bb36 commit a22241e

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ Errors
284284
----
285285
286286
On error, the library throws an error class indicating the type of error. The error classes are derived from the SQLITE3 error names, so if the error code is SQLITE_CONSTRAINT, the error class thrown is sqlite::exceptions::constraint. Note that all errors are derived from sqlite::sqlite_exception and that itself is derived from std::runtime_exception.
287+
sqlite::sqlite_exception has a get_code() member function to get the SQLITE3 error code.
287288
288289
```c++
289290
database db(":memory:");
@@ -297,7 +298,7 @@ On error, the library throws an error class indicating the type of error. The er
297298
/* if you are trying to catch all sqlite related exceptions
298299
* make sure to catch them by reference */
299300
catch (sqlite_exception& e) {
300-
cerr << e.what() << endl;
301+
cerr << e.get_code() << ": " << e.what() << endl;
301302
}
302303
/* you can catch specific exceptions as well,
303304
catch(sqlite::exceptions::constraint e) { } */

hdr/sqlite_modern_cpp.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ namespace sqlite {
3030

3131
class sqlite_exception: public std::runtime_error {
3232
public:
33-
sqlite_exception(const char* msg, char code = -1): runtime_error(msg), code(code) {}
34-
sqlite_exception(char code): runtime_error(sqlite3_errstr(code)), code(code) {}
35-
char get_code() {return code;}
33+
sqlite_exception(const char* msg, int code = -1): runtime_error(msg), code(code) {}
34+
sqlite_exception(int code): runtime_error(sqlite3_errstr(code)), code(code) {}
35+
int get_code() {return code;}
3636
private:
37-
char code;
37+
int code;
3838
};
3939

4040
namespace exceptions {

tests/exceptions.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ int main() {
1717
// inserting again to produce error
1818
db << "INSERT INTO person (id,name) VALUES (?,?)" << 1 << "jack";
1919
} catch (sqlite_exception& e) {
20-
cerr << e.what() << endl;
20+
cerr << e.get_code() << ": " << e.what() << endl;
2121
expception_thrown = true;
2222
} catch (...) {
2323
cerr << "Ok, we have our excpetion thrown" << endl;

0 commit comments

Comments
 (0)