Any way to peek at the exception currently being thrown?
Steven Schveighoffer
schveiguy at gmail.com
Thu Jun 17 20:35:42 UTC 2021
On 6/1/21 12:36 PM, Andrei Alexandrescu wrote:
> One possible idiom is to use scope(failure) and take a look at the
> exception being thrown.
>
> Is there a way to do that? If not, I assume it shouldn't be difficult to
> add to druntime?
Thought of a great usecase for this (or something like it). This is
pretty much code from my application, I do this all over:
```d
auto conn = DB.getConnection;
conn.exec("START TRANSACTION");
scope(success) conn.exec("COMMIT");
scope(failure) conn.exec("ROLLBACK");
... // stuff
```
While this works, and isn't too horrible, what is horrible is the fact
that I can only do this ONCE. So I can't defensively use a reentrant
transaction troika as above. I have to rely on the top level doing the
transaction (which means this code gets repeated everywhere, and I can't
do it in helper functions).
I thought of making a type that automatically either commits or rolls
back based on whether we are throwing an exception or not. But there's
no way to figure that out in the destructor of the type.
But with some mechanism to say "what exception is in flight?" it would
be trivial.
I don't want to necessarily make it dependent on only SQL errors,
because other errors can happen, I want to rollback if ANY exceptions
are thrown.
Does someone know how to do this with current code? Essentially, I want
a reentrant transaction that either does commit or rollback whenever the
reentrance count goes to 0 and either an exception is being thrown
(rollback) or not (commit).
-Steve
More information about the Digitalmars-d
mailing list