Jump to page: 1 2
Thread overview
Detecting exception unwinding
Feb 03, 2016
Jonathan M Davis
Feb 03, 2016
Jonathan M Davis
Feb 04, 2016
Ali Çehreli
Feb 04, 2016
Jonathan M Davis
Feb 03, 2016
Jacob Carlborg
Feb 05, 2016
cy
Feb 06, 2016
cy
Feb 06, 2016
cy
February 03, 2016
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.

February 03, 2016
On Wednesday, February 03, 2016 11:09:00 Ola Fosheim Grøstad via Digitalmars-d-learn 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.

AFAIK, there is no way to detect whether an exception is in flight or not aside from the cases where scope(failure) or catch would catch the exception, and from what I recall of the last time that someone asked this question, the consensus was that it couldn't be done - but maybe I'm remembering incorrectly. I am pretty sure that this was asked within the last few months though, so if you search D.Learn, you can probably find that discussion.

- Jonathan M Davis


February 03, 2016
On Wednesday, 3 February 2016 at 11:41:28 UTC, Jonathan M Davis wrote:
> AFAIK, there is no way to detect whether an exception is in flight or not aside from the cases where scope(failure) or catch would catch the exception, and from what I recall of the last time that someone asked this question, the consensus was that it couldn't be done - but maybe I'm remembering incorrectly. I am pretty sure that this was asked within the last few months though, so if you search D.Learn, you can probably find that discussion.

:-/

I am looking for something like this:

http://en.cppreference.com/w/cpp/error/uncaught_exception

It is useful for certain types of libraries where you want to cancel out effects "undo commits" when exceptional situations arise.


February 03, 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
February 03, 2016
On Wednesday, 3 February 2016 at 12:44:39 UTC, Jacob Carlborg wrote:
> * 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.

Yes, if there is no such feature planned then maybe creating a new runtime is the most sensible option.

February 03, 2016
On Wednesday, February 03, 2016 11:47:35 Ola Fosheim Grøstad via Digitalmars-d-learn wrote:
> On Wednesday, 3 February 2016 at 11:41:28 UTC, Jonathan M Davis wrote:
> > AFAIK, there is no way to detect whether an exception is in flight or not aside from the cases where scope(failure) or catch would catch the exception, and from what I recall of the last time that someone asked this question, the consensus was that it couldn't be done - but maybe I'm remembering incorrectly. I am pretty sure that this was asked within the last few months though, so if you search D.Learn, you can probably find that discussion.
>
> :-/
>
> I am looking for something like this:
>
> http://en.cppreference.com/w/cpp/error/uncaught_exception
>
> It is useful for certain types of libraries where you want to cancel out effects "undo commits" when exceptional situations arise.

Yeah, and I'm pretty sure that we don't have anything like that at this point. Feel free to create an enhancement request for it:

https://issues.dlang.org

At least that way, it's kept track of, though I certainly have no idea when it might be implemented (presumably when someone needs it enough that they take the time to do so).

- Jonathan M Davis

February 03, 2016
On 02/03/2016 03:47 AM, Ola Fosheim Grøstad wrote:
> On Wednesday, 3 February 2016 at 11:41:28 UTC, Jonathan M Davis wrote:
>> AFAIK, there is no way to detect whether an exception is in flight or
>> not aside from the cases where scope(failure) or catch would catch the
>> exception, and from what I recall of the last time that someone asked
>> this question, the consensus was that it couldn't be done - but maybe
>> I'm remembering incorrectly. I am pretty sure that this was asked
>> within the last few months though, so if you search D.Learn, you can
>> probably find that discussion.
>
> :-/
>
> I am looking for something like this:
>
> http://en.cppreference.com/w/cpp/error/uncaught_exception
>
> It is useful for certain types of libraries where you want to cancel out
> effects "undo commits" when exceptional situations arise.
>
>

std::uncaught_exception used to be considered useless:

  http://www.gotw.ca/gotw/047.htm

Does that apply to D?

Ali

February 04, 2016
On Thursday, 4 February 2016 at 07:55:42 UTC, Ali Çehreli wrote:
> std::uncaught_exception used to be considered useless:
>
>   http://www.gotw.ca/gotw/047.htm
>
> Does that apply to D?

I've read that one, but Herb Sutter does not provide an argument, just a claim that having different semantics on the exceptional path to be somehow immoral.

February 04, 2016
On Wednesday, 3 February 2016 at 21:35:38 UTC, Jonathan M Davis wrote:
> https://issues.dlang.org
>
> At least that way, it's kept track of, though I certainly have no idea when it might be implemented (presumably when someone needs it enough that they take the time to do so).

Thanks, I think I will wait with filing an enhancement request as I think there are more pressing issues to be dealt with.  I should look into creating my own runtime anyway.

February 04, 2016
On Wednesday, February 03, 2016 23:55:42 Ali Çehreli via Digitalmars-d-learn wrote:
> std::uncaught_exception used to be considered useless:

I think that the only case I've ever had for it was for a unit testing framework where I wanted to use RAII to print something when the tests failed (and thus an exception was thrown) but not print if they succeeded, and if I were to do that in D, I'd just use scope(failure).

- Jonathan M Davis


« First   ‹ Prev
1 2