Detecting exception unwinding

Jacob Carlborg via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 3 04:44:39 PST 2016


On 2016-02-03 12:09, Ola Fosheim Grøstad wrote:
> Is there some reliable way to detect that a destructor is called because
> of exception unwinding?
>
> I basically want to change behaviour within a destructor based on
> whether the destructor is called as a result of a regular or an
> exceptional situation.
>
> E.g. commit changes to a database on regular destruction, or inhibit
> logging during exception unwinding.

I don't have an good solutions, but a few ideas that you could try:

* Try core.runtime.Runtime.traceHandler [1]. I'm assuming the trace 
handler will be called at some point in time if an exception has been thrown

* If you're on Linux or FreeBSD and using HEAD you could, I think, use 
"__cxa_current_exception_type" or "__cxa_uncaught_exception" since DMD 
now supports DWARF exception

* Worst case scenario, override "_d_throwc" [2]

For the trace handler and overriding "_d_throwc" you would just use the 
default implementation plus store a boolean (or counter) in a TLS 
variable indicating an exception has been thrown.

[1] http://dlang.org/phobos/core_runtime.html#.Runtime.traceHandler
[2] 
https://github.com/D-Programming-Language/druntime/blob/1f957372e5dadb92ab1d621d68232dbf8a2dbccf/src/rt/deh_win64_posix.d#L213

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list