Hi there,
To me it's a bit unclear in which case I should use Connection_getLastError(). And, when using Exception_frame.message, if it makes sense to catch a specific exception.
What I mean is:
TRY { Connection_executeQuery(...); } CATCH (SQLException) { printf("SQLException -- %sn", Connection_getLastError(c)); }
versus TRY { Connection_executeQuery(...); } CATCH (SQLException) { printf("SQLException -- %sn", Exception_frame.message); }
versus
TRY { Connection_executeQuery(...); } ELSE { printf("Exception -- %sn", Exception_frame.message); }
Ok, I understand that ELSE { } will catch every exception, so I can't safely use Connection_getLastError() there.
But with CATCH(SQLException), is there a situation where Connection_getLastError() is preferred over Exception_frame.message?
Cheers, Till
On 23 Jun 2025, at 17:37, g4-lisz@tonarchiv.ch wrote:
1. There was a time when libzdb did not use exceptions, and Connection_getLastError() harked back to that time. Today, always prefer Exception_frame.message. Connection_getLastError() is now more of an internal function.
Connection_getLastError() returns any database errors (or “?”) on the current connection, while Exception_frame.message returns database errors _and_ API errors such as parameter or column index out of bounds, pool is full, etc.
2. BTW, don’t bother catching individual Exceptions; just use ELSE to make the code simpler, unless you want to fail on an AssertException. I.e. use TRY-ELSE:
TRY { // Code that can throw exception } ELSE { printf("Exception -- %s\n", Exception_frame.message); } END_TRY;
If you use TRY-CATCH (SQLException) then you will also not catch any AssertException and the app with call the Abort Handler, if installed, or abort.
so I can't safely use Connection_getLastError() there.
You can safely call Connection_getLastError() from anywhere, but unless there was a database error it might not return anything useful.
Jan-Henrik
Hi Jan-Henrik
Thank you so much for your detailed explanation! That completely cleared up the question for me.
And I adjusted my code accordingly. I.e. I replaced all the CATCH() and Connection_getLastError().
Cheers, Till
June 24, 2025 4:29 PM, "Jan-Henrik Haukeland" hauk@tildeslash.com wrote:
On 23 Jun 2025, at 17:37, g4-lisz@tonarchiv.ch wrote:
- There was a time when libzdb did not use exceptions, and Connection_getLastError() harked back
to that time. Today, always prefer Exception_frame.message. Connection_getLastError() is now more of an internal function.
Connection_getLastError() returns any database errors (or “?”) on the current connection, while Exception_frame.message returns database errors _and_ API errors such as parameter or column index out of bounds, pool is full, etc.
- BTW, don’t bother catching individual Exceptions; just use ELSE to make the code simpler, unless
you want to fail on an AssertException. I.e. use TRY-ELSE:
TRY { // Code that can throw exception } ELSE { printf("Exception -- %s\n", Exception_frame.message); } END_TRY;
If you use TRY-CATCH (SQLException) then you will also not catch any AssertException and the app with call the Abort Handler, if installed, or abort.
so I can't safely use Connection_getLastError() there.
You can safely call Connection_getLastError() from anywhere, but unless there was a database error it might not return anything useful.
Jan-Henrik