473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Try catch in ctor initialization list

Hallo,

few days ago I see ctor like this:

Ctor() try : v1(0) {
// some code
}
catch(...) {
// some code
}

This is something realy shocking for me. So I write something like:

class Kljasa {
int *bvarijabla;
public:
// first ctor
Kljasa(int cvarijabla) try {
bvarijabla = new int[cvarijabla];
}
catch(...) {
cout << "CTOR DOWN" << endl;
bvarijabla = NULL;
}

//second like first with try block inside
Kljasa(int cvarijabla, int dummy) {
try {
bvarijabla = new int[cvarijabla];
}
catch(...) {
cout << "CTOR DOWN" << endl;
bvarijabla = NULL;
}
}
};

If I write:

Kljasa k(-10, 1); // second ctor

everithing is OK - exception is catched and programm continues to execute
(I know that this ctor-catching is not good solution but suppose that it's
ok).

Now, when I try to call first ctor my program
crashs with "abnormal program termination" but catch block displayed "CTOR
DOWN". It sems like
catch block rethrow exception again?!

My qusetion is: what first ctor realy do if exception is throwed?

Note, that I tested this only with MinGW 3.4.2.

Thanks,
Zaharije Pasalic
Dec 2 '05 #1
5 3008
PasalicZaharije wrote:

My qusetion is: what first ctor realy do if exception is throwed?


You need to ask the experts for your compiler, since this
is a nonstandard enhancement of your compiler.

--
Karl Heinz Buchegger
kb******@gascad.at
Dec 2 '05 #2
Hi

Karl Heinz Buchegger wrote:
You need to ask the experts for your compiler, since this
is a nonstandard enhancement of your compiler.


I'm not sure what you mean.
However, this is simply undefined behaviour, as:

"Referring to any non-static member or base class of an object in the
handler for a function-try-block of constructor or destructor for that
object results in undefined behavior."

These objects have all been destroyed before the catch-block is entered.

Markus
Dec 2 '05 #3
PasalicZaharije wrote:
Hallo,

few days ago I see ctor like this:

Ctor() try : v1(0) {
// some code
}
catch(...) {
// some code
}

This is something realy shocking for me. So I write something like:

class Kljasa {
int *bvarijabla;
public:
// first ctor
Kljasa(int cvarijabla) try {
bvarijabla = new int[cvarijabla];
}
catch(...) {
cout << "CTOR DOWN" << endl;
bvarijabla = NULL;
This last statement is both pointless and technically illegal. It is
pointless because the constructor is failing by an exception (and using
a function-try-block ALWAYS ends with an exception; see below), which
means that the object and its members don't exist -- i.e., the memory
allocated for bvarijabla is about to be returned to the memory pool
from which it came (usually the stack or heap) and can never be
accessed. Thus setting it to NULL does nothing for you or your user.
Accessing bvarijabla in the catch block is illegal according to the
Standard because all class members are out of scope in the catch block.
Since it apparently built for you, however, this represents a
non-conformancy or bug in your compiler.
}

//second like first with try block inside
Kljasa(int cvarijabla, int dummy) {
try {
bvarijabla = new int[cvarijabla];
}
catch(...) {
cout << "CTOR DOWN" << endl;
bvarijabla = NULL;
}
}
};

If I write:

Kljasa k(-10, 1); // second ctor

everithing is OK - exception is catched and programm continues to execute
(I know that this ctor-catching is not good solution but suppose that it's
ok).

Now, when I try to call first ctor my program
crashs with "abnormal program termination" but catch block displayed "CTOR
DOWN". It sems like
catch block rethrow exception again?!

My qusetion is: what first ctor realy do if exception is throwed?

Note, that I tested this only with MinGW 3.4.2.

Thanks,
Zaharije Pasalic


Check out this link:

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

It discusses the uses and limitations of function-try-blocks. And yes,
the function-catch block ALWAYS exits by throwing an exception --
either explicitly (i.e., you rethrow the exception that was caught or
throw some other exception) or implicitly (i.e. if you let control pass
to the end of catch block without throwing it implicitly rethrows the
exception that was caught). This is standard behavior for
function-try-blocks. As that link argues, the only practical use for
function-try-blocks is to translate one exception type to another.

Your second constructor could catch a failed new, but it could not
catch an exception emitted by the constructor of a base class of Kljasa
or by the constructor of a member of Kljasa:

class MyException {};
class Throws { Throws() { throw MyException(); } };

class Kljasa
: public Throws
{
Throws throws_;
public:
Kljasa()
: Throws()
, throws_()
{
try { }
catch( const MyException& e )
{
// Control will never reach here
}
}
};

Here, both the member throws_ and the base Throws (which needn't be
listed explicitly in the constructor initialization list because their
default constructors would automatically be invoked in the same way,
but which are listed for purposes of illustration) would throw but
neither exception could be caught by the constructor.

Cheers! --M

Dec 2 '05 #4
mlimber wrote:
PasalicZaharije wrote:
Hallo,

few days ago I see ctor like this:

Ctor() try : v1(0) {
// some code
}
catch(...) {
// some code
}

This is something realy shocking for me. So I write something like:

class Kljasa {
int *bvarijabla;
public:
// first ctor
Kljasa(int cvarijabla) try {
bvarijabla = new int[cvarijabla];
}
catch(...) {
cout << "CTOR DOWN" << endl;
bvarijabla = NULL;


This last statement is both pointless and technically illegal. It is
pointless because the constructor is failing by an exception (and using
a function-try-block ALWAYS ends with an exception; see below), which
means that the object and its members don't exist -- i.e., the memory
allocated for bvarijabla is about to be returned to the memory pool
from which it came (usually the stack or heap) and can never be
accessed. Thus setting it to NULL does nothing for you or your user.
Accessing bvarijabla in the catch block is illegal according to the
Standard because all class members are out of scope in the catch block.
Since it apparently built for you, however, this represents a
non-conformancy or bug in your compiler.

[snip]

Oops. Members are not out of scope, but as Markus Moll said elsewhere
on this thread, accessing them does result in the dreaded UB --
undefined behavior.

Cheers! --M

Dec 2 '05 #5

Karl Heinz Buchegger wrote:
PasalicZaharije wrote:

My qusetion is: what first ctor realy do if exception is throwed?


You need to ask the experts for your compiler, since this
is a nonstandard enhancement of your compiler.


The OP has the syntax for function-try-blocks wrong, but the feature
does exist in the standard.

Cheers! --M

Dec 2 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jacek Dziedzic | last post by:
Hi! A) Why isn't it possible to set a member of the BASE class in an initialization list of a DERIVED class constructor (except for 'calling' the base constructor from there, of course)? I even...
6
by: Jackson A. Marshall | last post by:
I have a been testing the try/catch exception mechanism (as implemented by DigitalMars) and found that if I throw an object its dtor gets called 3 times, if I throw a reference its dtor gets...
3
by: Pelle Beckman | last post by:
Hi all, I have a few - beginners - questions: * Why can't I put object references in a std::vector, i.e std::vector<MyClass&> ? At least in doesnt work in gcc (mingw, win32) * What's the...
2
by: Matthias Kaeppler | last post by:
Hi, say I have an arbitrary class Bar: 1 Bar a; 2 Bar b(a); 3 Bar c = a; In line 3, is the default ctor called for c _first_ and _then_ the assignment operator, or is c never default...
12
by: Andrew Schepler | last post by:
When compiled with Visual C++ .NET 2003 (only), the program below aborts as though no matching catch clause is present. If the copy constructor of A is made public, it successfully catches the...
5
by: Grahamo | last post by:
Hi, I have a basic question regarding some legacy code I'm working with; Basically the code looks something like this. I'd like to know if there are any reasons why a particular approach is...
4
by: Oliver S | last post by:
I got a class, a VCL form actually. I have two const member that I am initializing (in the order they were declared). My question is, do I put them before calling inherited constructor or after? ...
9
by: utab | last post by:
Dear all, How do experienced programmers using this group use the rule of thumb,namely copy ctor, assignment operator, and dtor. example if the class does not need them dont use and define...
3
by: John Salmon | last post by:
g++ complains about illegal access to a private member when the following is compiled with a private copy constructor for the class C. When the copy constructor is public, the program runs and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.