Re: Clang & C++ exceptions in destructors
Re: Clang & C++ exceptions in destructors
- Subject: Re: Clang & C++ exceptions in destructors
- From: Jesper Papmehl-Dufay <email@hidden>
- Date: Tue, 08 Jan 2013 15:30:14 +0100
Thank you!
I suspected it might be something new in C++11.
Decorating the few destructors I want to allow to throw with “noexcept(false)” solves the problem nicely.
Thanks again!
/Jesper
8 jan 2013 kl. 14.55 skrev Jean-Daniel Dupas:
> I remember now.
>
> Destructor in C++11 are implicitly declared as noexcept. That's why you show this behavior.
>
> To change this behavior, you have to add an explicit noexcept declaration.
>
> public: ~CThrowInDestructor() noexcept(false) {
> throw std::exception();
> }
>
> Le 8 janv. 2013 à 14:50, Jean-Daniel Dupas <email@hidden> a écrit :
>
>> Yep.
>>
>> Changing from C++11 to C++98 change the behavior.
>> It terminates in C++11 mode, and throw the exception in C++98.
>>
>> That said, I can't tell if this is expected or not.
>>
>> Le 8 janv. 2013 à 12:07, Jesper Papmehl-Dufay <email@hidden> a écrit :
>>
>>> Interesting! I am using Xcode 4.5.2 too. Maybe we have different compiler settings? Enclosed is the complete project that reproduces the problem for me.
>>>
>>> Thanks!
>>> /Jesper
>>> <throw.zip>
>>>
>>>
>>> 8 jan 2013 kl. 12.01 skrev Jean-Daniel Dupas:
>>>
>>>>
>>>> Le 8 janv. 2013 à 11:27, Jesper Papmehl-Dufay <email@hidden> a écrit :
>>>>
>>>>> It seems clang (or the “Apple LLVM Compiler”) doesn’t like it when I throw exceptions from a C++ destructor. Is this intentional, or should I report it as a bug?
>>>>> (I aware that in general, throwing from a destructor is a very bad idea, I’m only doing it in very specific cases. For example, I know that my destructor is not called during stack unwinding due to another exception.)
>>>>>
>>>>> For example, the following program will terminate with a “terminate called without an active exception” message when the destructor throws std::exception (testing using Xcode 4.5.2 on Mac OS X 10.7.5):
>>>>>
>>>>>
>>>>> #include <stdexcept>
>>>>>
>>>>> class CThrowInDestructor {
>>>>> public: CThrowInDestructor() {}
>>>>> public: ~CThrowInDestructor() {
>>>>> throw std::exception();
>>>>> }
>>>>> };
>>>>>
>>>>> int main(int argc, const char * argv[]) {
>>>>> CThrowInDestructor* instance = new CThrowInDestructor;
>>>>> try {
>>>>> delete instance;
>>>>> instance = NULL;
>>>>> }
>>>>> catch (...) {
>>>>> }
>>>>> return 0;
>>>>> }
>>>>>
>>>>>
>>>>> If I switch compiler to LLVM GCC 4.2, the program exits normally, so it seems the problem is specific to clang.
>>>>
>>>>
>>>> What version of Xcode are you using ? With Xcode 4.5.2, it behaves as expected.
>>>> I added a print statement in the catch clause in your sample, and it is properly printed before the program exits normally.
>>>>
>>>> -- Jean-Daniel
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>
>> -- Jean-Daniel
>>
>>
>>
>>
>
> -- Jean-Daniel
>
>
>
>
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden