|
Sure, Maxwell. I have posted below.
regards,
George
|
|
|
|
|
Okay, from observing the hundreds of your everyday questions, I guess that you have been having difficulty to understand what every authors (MSDN and C++ books) want to express. Maybe you should pick up another beginner level book first.
Here is the example for your original question.
class A
{
public:
A() {
cout << "A(), and going to throw! \n";
throw 3;
}
};
A obj;
void main()
try {
}
catch(...) {
}
Maxwell Chen
|
|
|
|
|
Hi Maxwell,
Here is my code to verify exception from constructor of global variable could be caught and handled. Anything wrong?
using namespace std;
class Foo{
public:
Foo ();
};
Foo::Foo()
try{
cout << "constructing" << endl;
throw(0);
} catch (...)
{
cout << "catch exception in constructor" << endl;
}
Foo foo;
int main()
{
int i = 0;
cout << i;
return 0;
}
regards,
George
|
|
|
|
|
Your way is local catch (in the scope of the constructor), not what Stroustrup wanted to say: Global catch.
Maxwell Chen
|
|
|
|
|
Try this, and it won't compile.
try {
A obj;
}
catch(...) {
}
void main()
{
}
Maxwell Chen
|
|
|
|
|
Hi Maxwell,
Can you post all the code please? So that we can see where is the compile error.
regards,
George
|
|
|
|
|
By following my first reply in this thread.
The class A sample.
Maxwell Chen
|
|
|
|
|
Sure Maxwell,
You can not put try/catch just around the global variable, but put try/catch into a function, constructor (my sample) or main.
regards,
George
|
|
|
|
|
Thanks Maxwell,
I have read again the chapter and Bjarne only mentioned, "there is no way to catch exceptions from the initialization of global variables (section 14.7)".
No words like local or global catch. Do you have the book at hand?
regards,
George
|
|
|
|
|
George_George wrote: No words like local or global catch.
Please look it up in Cambridge Dictionary for the definitions of "local" and "global"!
Maxwell Chen
|
|
|
|
|
Thanks Maxwell,
I have taken time to type down all the original words here,
--------------------
This will catch every exception, except those thrown by construction and destruction of global variables. There is no way of catching exceptions thrown during initialization of global variables. The only way of gaining control in case of throw from an initializer of a nonlocal static object is set_unexpected ($14.6.2). This is another reason to avoid global variable whenever possible.
--------------------
You can see there is no terms like local or global.
regards,
George
|
|
|
|
|
Then what do you think about "... catching exceptions thrown from global variables"?
Maxwell Chen
|
|
|
|
|
Thanks Maxwell,
My understanding is catching exceptions from initialization (constructor) of global variables. Since the whole sentence is "There is no way of catching exceptions thrown during initialization of global variables".
Anything wrong?
regards,
George
|
|
|
|
|
George_George wrote: "there is no way to catch exceptions from the initialization of global variables (section 14.7)"
Well, for instance, I've not the book at hand, but I'm able to tell "from the initialization" from "inside the initialization".
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 taken time to type down all the original words here,
--------------------
This will catch every exception, except those thrown by construction and destruction of global variables. There is no way of catching exceptions thrown during initialization of global variables. The only way of gaining control in case of throw from an initializer of a nonlocal static object is set_unexpected ($14.6.2). This is another reason to avoid global variable whenever possible.
--------------------
Any comments after reading this and reviewing my code sample above?
regards,
George
|
|
|
|
|
George_George wrote: This will catch every exception, except those thrown by construction and destruction of global variables. There is no way of catching exceptions thrown during initialization of global variables. The only way of gaining control in case of throw from an initializer of a nonlocal static object is set_unexpected ($14.6.2). This is another reason to avoid global variable whenever possible.
Please mind that the subject (the 1st word, "This") is the below.
int main()
try {
}
catch(std::range_error) {
}
catch(std::bad_alloc) {
}
catch(...) {
}
Maxwell Chen
|
|
|
|
|
Sure, Maxwell!
In your above approach, in main there is no way of catching exceptions from constructor of global variables.
But, it does not mean there is no way to catch exceptions from constructor of global variables, like I showed above -- besides the method of catching in main as you showed above. Agree?
regards,
George
|
|
|
|
|
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]
|
|
|
|