473,397 Members | 1,972 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,397 software developers and data experts.

exception handling within initialization list

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
Jul 23 '05 #1
3 2775
Tony Johansson wrote:

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. "

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2
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.

Jul 23 '05 #3
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(...)
{
}

}

Jul 23 '05 #4

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

Similar topics

7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
3
by: Tony Johansson | last post by:
Hello Experts! Assume foo is a method in a some class or a stand-alone function and method push is throwing an exception of class logic_error. Assume also that I want to handle this exception in...
4
by: Robbie Hatley | last post by:
I've been playing with "auto_ptr" and the "Resource Acquisition Is Initialization" concept. On page 199 of Lippman's book "Effective C++" he says "All active local class objects of a function are...
7
by: Dan Bass | last post by:
In a somewhat complex application, I've developed plug-in architecture and am having a problem as to when to catch general exceptions for logging purposes. In each plug-in class library, for...
1
by: Noor | last post by:
Hi all, I am trying to catch all types of exceptions from a app regardless of whether it is in debugger mode( VS development environment) or run the.exe file outside the IDE. My App...
7
by: ZorpiedoMan | last post by:
Well, I'm still wondering around in the "land of the lost exception"... It seems that controls that are bound to a class and the class throws an error in the SET method of the bound member, the...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
2
by: Carol | last post by:
Exception may be thrown in the code inside the try block. I want to handling the SqlException with State == 1 in a special way, and for all others I want to use a general way to handle. Which of...
4
by: Sunil Varma | last post by:
Hi, Here is a piece of code where the constructor throws an exception. class A { int n; public: A() try{
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.