exception handling within initialization list

T

Tony Johansson

Hello!

I'm reading in a book about C++ and there is something that I want to ask
you about.

It says "since the constructor does not return any value, the only way to
handle errors that
occur during its execution is to use exception handling. You can deal with
errors
that occur during the execution of the constructor's body by using try block
within this body. The constructor can catch exception thrown by other
constructors invoked from the member initialization list."
I understand the text but not the bit of code below. Why having a try block
here
This bit of code is from the book. There can't exist any exception within
the try block

void Student::Student(long number, string name)
try
: number_(number), name_(name){
}
catch exc& e){
.....
}

//Tony
 
K

Karl Heinz Buchegger

Tony said:
Hello!

I'm reading in a book about C++ and there is something that I want to ask
you about.

It says "since the constructor does not return any value, the only way to
handle errors that
occur during its execution is to use exception handling. You can deal with
errors
that occur during the execution of the constructor's body by using try block
within this body. The constructor can catch exception thrown by other
constructors invoked from the member initialization list."
I understand the text but not the bit of code below. Why having a try block
here
This bit of code is from the book. There can't exist any exception within
the try block

void Student::Student(long number, string name)
try
: number_(number), name_(name){
}
catch exc& e){
....
}

imageine that the system has run out of memory and the std::string
class throws an exception when it tries to initialize itself with the
passed name.

Your book says it clearly:

" The constructor can catch exception thrown by other
constructors invoked from the member initialization list. "
 
P

publictom

The initialisers are expressions, and can include function calls,
dynamic casts, creation of objects using the new keyword, and other
things that can throw exceptions. For example you could have
number_(f()), where f() is a function that can throw an exception,
instead of number_(number).

There is a guru of the week about when to use this construct at
http://www.gotw.ca/gotw/066.htm.
 
K

kannansekar

Lets modify your code to reflect a 'REAL' exception case

void Student::Student(TYPE1 param1, TYPE2 param2)
try
: member_variable_type1(param1), member_variable_type2(param2){
//here what happens is that the constructor (may be copy constructor )
//will be invoked twice for each member variable declared

}

But you can ask why dont we do like this instead of above

void Student::Student(TYPE1 param1, TYPE2 param2)
{
//this will not be EXACTLY as the above because,
//at this POINT The OBJECT WOULD HAVE BEEN CREATED COMPLETELY
//But the above construct , the object (STUDENT) still in its nascent
condition
try
{
member_variable_type1=param1
member_variable_type2=param2
}
catch(...)
{
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,203
Messages
2,571,059
Members
47,668
Latest member
SamiraShac

Latest Threads

Top