|
Yes, and they're really the same as the unexpected handler is just like an ultimate catch block for when all others have failed to catch. If you want to understand how this works in the real world, as opposed to how Bjarne says it should work, then take a look at the source code for the C Runtime that ships with Visual Studio. C++ exception handling in the MS world relies on both the runtime and special code injected into your functions by the Compiler. There's also a good article on here somewhere called something like How C++ Exception Handling Really Works. Enjoy
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Hi Matthew,
I can not find the article you mentioned "How C++ Exception Handling Really Works" from Google. Could you provide a link please?
regards,
George
|
|
|
|
|
My mistake George the title is somewhat different. This[^] is the one I'd seen a link to. It comes up every now and then in the list of related popular articles at the bottom of one page or another on CP. If you soak up all that you'll know more about exception handling than I do so you can answer the next query that comes up in that area
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Thanks Matthew,
Great article! About my original question, do you agree?
--------------------
For (2), in unexpected handler, since the input parameter is null, so in order to get the exception information, we need to re-throw it and catch it to get exception informaiton.
Is my understanding correct?
--------------------
regards,
George
|
|
|
|
|
Hello everyone,
The following swap technique is used to make assignment operator exception safe (means even if there is exception, the current object instance's state is invariant).
It used a temporary object "temp" in this sample, and assignment is made on a to temp ar first. Even if there is exception, the current this object's state is not corrupted.
My question is, the pattern works only if there is no exception thrown by swap function. If there are exception in swap function, the state of current object instance may still be corrupted (swap may invoke the assignment operator of member variables). Is my understanding correct?
class A;
A& A::operator= (const A& a)
{
A temp;
temp = a;
swap (*this, temp);
return *this;
}
thanks in advance,
George
|
|
|
|
|
Hey George, wait a moment, throw out your book, that's recursive junk!!!!!!!!!!!
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Why CPallini? More information please?
regards,
George
|
|
|
|
|
George you're a quite smart guy: I think you'll soon reckon than calling the equal operator inside its very definition causes a theoretical endless function call loop and a practical stack overflow.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Hi CPallini,
I agree and I think it depends on how swap is implemented. If swap is implemented by assignments of POD types, I think it is safe.
regards,
George
|
|
|
|
|
Nope, my friend, the line
temp = a;
causes infinite recursion no matter how swap is implemented.
BTW could you please post the original B. Stroustrup's sample (toxcct told me about... )?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Hi CPallini,
This is not Bjarne's sample.
This question is what I learned that swap could be used to design and implement an exception safe assignment operator. Yes, there is some issues in my code.
I think you are well known about how to use swap to implement exception safe assignment opeator from your replies. Could you help to correct my code please?
regards,
George
|
|
|
|
|
George_George wrote: This question is what I learned that swap could be used to design and implement an exception safe assignment operator.
Well, I think you learned about reading any book (or article). Please reference it, if you can.
George_George wrote: I think you are well known about how to use swap to implement exception safe assignment opeator from your replies. Could you help to correct my code please?
In fact, no. I've grasped the main idea from your sample code, but I wasn't aware of that technique, hence I cannot try to fix the code simply guessing about (if it is a well established pattern, then we need a state-of-art example)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Thanks for your comment, CPallini!
Let us wait for other MVP's reply.
regards,
George
|
|
|
|
|
George_George wrote: Let us wait for other MVP's reply.
And, of course, for replies of any other member.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Yes. but I believe that the state of the parameter only may throw an exception, if its state is corrupted.
that's why we do a temporary assignement (temp = a ) to check first if assigning a to something else would not crash.
so if a is OK, then swapping it with this would be OK too...
|
|
|
|
|
Well, even ignoring the recursion madness of George sample code, I think that swap may take care about, since it will perhaps be coded in a way similar to the following:
A temp;
temp = a;
a = *this;
*this = temp;
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
that's how swap is normally designed.
however, there's a lack in this design, because having a A::operator=() calling a A::swap() function, which itself calls A::operator=() 3 more times is recursive for sure, with no possibility to stop the loop.
so I think swap is not coded like this in B. Stroustrup's example...
|
|
|
|
|
toxcct wrote: that's how swap is normally designed.
The above I was suggesting: hence no need to create a temporary assignment before it.
toxcct wrote: however, there's a lack in this design, because having a A::operator=() calling a A::swap() function, which itself calls A::operator=() 3 more times is recursive for sure, with no possibility to stop the loop.
so I think swap is not coded like this in B. Stroustrup's example...
I agree that there is recursion in the sample code (after all I was the first pointing out it...) and I was not suggesting to take that code as it stands, suppose, for instance, that method name was foo , instead of operator== .
BTW I think that swap should rely on the assignment operator and not the opposite (but I've not Stroustrup's example, that of course I suppose correct, at hand, I think George is not a diligent poster recently... )
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Thanks CPallini,
I agree that there is some issue in my code. It is appreciated if you could provide some pseudo code to correct my code to implement the using swap technique to implement a exception safe assignment operator.
regards,
George
|
|
|
|
|
George_George wrote: I agree that there is some issue in my code.
Some issue? Some issue? Some issue?
I won't call 'some issue' going into infinite recursion while aiming to provide an 'exception safe' operator.
Just kidding...
George_George wrote: It is appreciated if you could provide some pseudo code to correct my code to implement the using swap technique to implement a exception safe assignment operator.
Oh no, you made the original post: that's your side of the game.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Hi CPallini,
I have searched some answers, but I do not think it works. Because either copy constructor or swap will potentially call assignment operator again.
Any ideas?
A& A::operator=(const A& other)
{
A temp(other);
swap(temp);
return *this;
}
regards,
George
|
|
|
|
|
hey, you want an exception safe copy ?
backup your object, then assign it the new object, and if it fails, restore its backup !
|
|
|
|
|
Thanks toxcct,
I agree with the general rule. But how to avoid the recursive invocation of assignment operator in your comments? Could you provide some code please?
regards,
George
|
|
|
|
|
toxcct wrote: hey, you want an exception safe copy ?
backup your object,
or take a picture of it.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
[my articles]
|
|
|
|
|
Hi toxcct,
I think in such pattern, swap should be implemented to a list of assignments of POD types, right?
BTW: if I am wrong, please help to correct code in your senses.
regards,
George
|
|
|
|