|
On that page, Stroustrup is not talking about catching exceptions inside a ctor or dtor, okay?! For that kind of approach, it is noted in another section.
Maxwell Chen
|
|
|
|
|
George_George wrote: Any comments after reading this and reviewing my code sample above?
Yes, the reported original words confirm my observation: if a constructor throws and then catches itself, outside of the construction process you see nothing, hence Bjarne obviously refers to exceptions thrown by global object constructors AND not catched inside the constructors themselves.
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]
|
|
|
|
|
We should ask Chris to add a new emoticon: one where there is a head banging on a wall...
|
|
|
|
|
Hi Cedric,
If you have any confusions, please share with us.
1. I do not think I am always correct. And I am happy to learn from you and correct myself.
2. I do not think Bjarne is always correct -- at least from literal point of view.
regards,
George
|
|
|
|
|
George, I suggest you to attend some English class.
Maxwell Chen
|
|
|
|
|
From commucating with you guys, I improved my English.
regards,
George
|
|
|
|
|
... (I refuse to speak in English.)
Maxwell Chen
|
|
|
|
|
George_George wrote: I do not think I am always correct
But you need a very big amount of argumentations to give up.
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]
|
|
|
|
|
Definitely.
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]
|
|
|
|
|
No CPallini,
1.
CPallini wrote: Yes, the reported original words confirm my observation: if a constructor throws and then catches itself, outside of the construction process you see nothing,
We can do anything as we like to do in exception handler, like write log.
2.
CPallini wrote: hence Bjarne obviously refers to exceptions thrown by global object constructors AND not catched inside the constructors themselves.
If it were his point, I have no further question. But my original understanding is, there is no way to catch exceptions from constructor of global variables -- not the limitatino of catching exceptions in main.
regards,
George
|
|
|
|
|
George_George wrote: We can do anything as we like to do in exception handler, like write log.
Yes we you can. But this is NOT the point.
We cannot impose to all people that develop classes for us to write logs. I mean Bjarne refers (probably) to a generic scenario: you haven't control to internal details of the global object you're instancing hence, if it simply throws, you have no chance to catch thrown exception.
George_George wrote: But my original understanding is, there is no way to catch exceptions from constructor of global variables -- not the limitatino of catching exceptions in main.
Your original understanding is probably wrong (on statistical grounds too ).
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,
It makes more senses. You mean we can not access the code of constructor of the class of the global variable and in its constructor, it is not written in a way that constructor can handle the exception by itself by throw outside?
CPallini wrote: you haven't control to internal details of the global object you're instancing hence
regards,
George
|
|
|
|
|
I mean the general scenario is:
You're using an object whose constructor may throw (to the outside world!) or not and you cannot (or don't want or whatever...) change its constructor internal details.
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. Your reply is one of the most great replies in this discussion.
regards,
George
|
|
|
|
|
The original layout of that page is below.
...... For example:
int main()
try {
}
catch(std::range_error) {
}
catch(std::bad_alloc) {
}
catch(...) {
}
This will catch every exceptions, except those thrown
by ctor or dtor of global variables.
Maxwell Chen
|
|
|
|
|
Hi Maxwell,
If this were Bjarne's point, I will have no question. But as I showed to you, there is ways to catch exception from constructor of global variable -- so to say "there is no way of catching exceptions thrown from initialization of a nonlocal static object".
regards,
George
|
|
|
|
|
George_George wrote: If this were Bjarne's point, I will have no question.
You can email Stroustrup!
George_George wrote: But as I showed to you, there is ways to catch exception from constructor of global variable
Do you think I don't know the way catching inside the ctor?! I have been using C++ for ten years.
Maxwell Chen
|
|
|
|
|
George_George wrote: I have read again the chapter and Bjarne only mentioned,
Stop messing with Bjarne's book. It is more of a reference book. It would be of great use after you've mastered the language. You seriously need a beginners book.
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
Anyway, how do you understand his (Bjarne) points?
regards,
George
|
|
|
|
|
George_George wrote: Anyway, how do you understand his (Bjarne) points?
Probably a bit better than you.
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 not write down his points and correct me (make me convinced)?
regard,
George
|
|
|
|
|
Because we surrendered!
Maxwell Chen
|
|
|
|
|
Did you even read his post ?
The problem is simply that if the constructor throws an exception, you'll never be able to catch it if it is a global object. That's what the author was speaking about. Not about something that you can catch in your constructor.
What's the point of your example ?? You throw something that is caught two lines under. Why do you need to throw an exception for such a thing ? That's just totally not adapted.
But now, how can you inform the users of your object that something went wrong during construction of the object ? Well, in general you throw an exception out of your constructor.
|
|
|
|
|
Thanks Cedric,
1.
Cedric Moonen wrote: What's the point of your example ?? You throw something that is caught two lines under. Why do you need to throw an exception for such a thing ? That's just totally not adapted.
I simulate exception from real world. In real word of constructor, there may be exceptions like bad_alloc something, in this sample, I make it easy by using throw (0) just to show my concept.
2.
Cedric Moonen wrote: But now, how can you inform the users of your object that something went wrong during construction of the object ? Well, in general you throw an exception out of your constructor.
In the exception handler of constructor.
3.
How do you understand Bjarne's point? As I showed, exception in initialization of constructor can be caught and handled.
regards,
George
|
|
|
|
|
George_George wrote: In the exception handler of constructor.
Sorry, but that doesn't make sense at all. By "users", I didn't mean real person that manipulate your program but part of your program that use your class. In that case, you would like to know that your object failed to construct so that you can take specific actions. The best way to do it is to throw an exception from within your constructor. Let's take an example:
void MyFunc<br />
{<br />
try<br />
{<br />
CDummyClass test();<br />
test.DoSomething();<br />
}<br />
catch (....)<br />
{<br />
}<br />
}
You cannot design all your classes so that they don't throw an exception in their constructor just because *somebody might be one day* using it as a global object.
And in general, in a specific object, you don't want to do something specific if an error occurs but let the user (part of the programm) manage the error in a specific way (because it knows how to handle the error).
|
|
|
|