Thread View: comp.std.c++
2 messages
2 total messages
Started by Michey Mehta
Tue, 30 Oct 1990 19:44
Question about exception handling
Author: Michey Mehta
Date: Tue, 30 Oct 1990 19:44
Date: Tue, 30 Oct 1990 19:44
52 lines
1651 bytes
1651 bytes
Hi,
This is a request for clarification about the exception handling
chapter in the ARM.
Consider the following example:
//////////////////////////////////////////////
class base {
public:
virtual void fixit(){};
};
class derived : base {
public:
virtual void fixit(){};
};
void func() {
base* object = new derived;
throw(*object);
}
main(){
try {
func();
}
catch(derived obj){
obj.fixit();
}
catch(base obj){
obj.fixit();
}
}
//////////////////////////////////////////////
The question is: should derived::fixit be invoked or base::fixit?
The ARM says on page 356 "A throw-expression initializes a temporary
object of the static type of the operand of throw and uses that temporary
to initialize the appropriately-typed variable in the handler".
1. Since the ARM says *static type*, can we cut the object back to
being a "base" object?
2. In that case, should we fall into the "catch (base obj)" ?
3. If so, isn't the vtbl entry for obj still pointing to derived::fixit,
which could lead to problems?
Michey Mehta
Hewlett-Packard California Language Lab. Internet: mnm@hpda.hp.com
Phone : (408) 447-5740 UUCP: ...!hplabs!hpda!mnm
Re: Question about exception handling
Author: Michey Mehta
Date: Tue, 30 Oct 1990 22:07
Date: Tue, 30 Oct 1990 22:07
21 lines
1086 bytes
1086 bytes
Hi, Looking at the ARM again, I realize that what it says is clear. A sequence such as: base* object = new derived; throw(*object); Should behave like this: base* object = new derived; base tmp_object = *object; //will invoke copy constructor for base throw(tmp_object); This throw will be caught by a catch(base). If the user wants to use polymorphism, then a pointer should be thrown and the catch clause should be "catch(base*)". Michey Mehta Hewlett-Packard California Language Lab. Internet: mnm@hpda.hp.com Phone : (408) 447-5740 UUCP: ...!hplabs!hpda!mnm
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads