Serving the Quantitative Finance Community

 
User avatar
JackBryan
Topic Author
Posts: 1
Joined: August 15th, 2010, 6:15 pm

Can a C++ program work well even though an exception is thrown from a class ?

May 26th, 2012, 5:47 pm

This is an interview question, the interview has been done. Given a class A with members of Class B and C. If an exception happens in class C's constructor but the program can still work well, what is the reason ? My answer:The class C's constructor is not implemented by A. Or, A does not have some instructions to perform some operations on class C. Exception is not an error. Exception handler function handle it well. Any better ideas ? thanks !
 
User avatar
Traden4Alpha
Posts: 3300
Joined: September 20th, 2002, 8:30 pm

Can a C++ program work well even though an exception is thrown from a class ?

May 26th, 2012, 5:59 pm

3) Instances of Class C never occur in real life.4) The specification for "working well" explicitly states that the process should die after a fatal exception (e.g., corrupted files or mal-formed inputs).
 
User avatar
CluelessCpp
Posts: 0
Joined: April 7th, 2012, 11:45 am

Can a C++ program work well even though an exception is thrown from a class ?

May 27th, 2012, 7:40 am

The exception is either caught (and handled) inside C's constructor, so the object can still be properly constructed.Or the exception is caught by the caller - but in this case the object of class A won't have been constructed (and any partially constructed sub-objects will have had their destructors called) and it's up to the caller to recover.
 
User avatar
dd3
Posts: 4
Joined: June 8th, 2010, 9:02 am

Can a C++ program work well even though an exception is thrown from a class ?

May 28th, 2012, 1:34 pm

I'd assume it's because that class A has a pointer to class C (probably B too) and that it uses a factory function to return new instances of A.The factory function handles any exceptions. I'm not sure it'd work if the class C member wasn't a pointer.
 
User avatar
bojan
Posts: 0
Joined: August 8th, 2008, 5:35 am

Can a C++ program work well even though an exception is thrown from a class ?

May 28th, 2012, 3:15 pm

See http://www.gotw.ca/gotw/066.htm . Many interview questions derive from GOTW, although sometimes they've been passed down through so many generations that the interviewers do not remember that any more...