943,915 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2001
  • C++ RSS
Feb 27th, 2007
0

C++ / Exceptions

Expand Post »
I'm having some trouble understanding how Exceptions thrown from constructors work...

Let's say I have a class like this:
C++ Syntax (Toggle Plain Text)
  1. class A {
  2. public:
  3. A() {
  4. if( error )
  5. throw MyExp;
  6. }
  7. }

If I try to define a variable of type "A" within a try {} catch {} block, I run into problems with scope:
C++ Syntax (Toggle Plain Text)
  1. int main() {
  2. try {
  3. A a_definition;
  4. } catch( Exp error ) {
  5. std::cout << error.message << endl;
  6. }
  7.  
  8. a_definition; // out of scope now...
  9. }

Wrapping the whole program in a try/catch block doesn't seem very appealing to me! And while I could do something like this:
C++ Syntax (Toggle Plain Text)
  1. A *a_ptr = NULL;
  2. try {
  3. a_ptr = new A;
  4. } catch( Exp error ) { // so on...

It doesn't look very clean; I don't need a pointer, other than for the work-around.

Is there an easy way to do this?
Last edited by Darkmeerkat; Feb 27th, 2007 at 1:05 am. Reason: Didn't know I could make the code blocks language-specific!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Darkmeerkat is offline Offline
12 posts
since Feb 2006
Feb 27th, 2007
0

Re: C++ / Exceptions

keep the constructor simple , do some basic initialization which can not fail, move rest of initialization to another function say init
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Feb 27th, 2007
0

Re: C++ / Exceptions

Click to Expand / Collapse  Quote originally posted by ithelp ...
keep the constructor simple , do some basic initialization which can not fail, move rest of initialization to another function say init
Thanks, I can do that.
Just out of curiosity, is there any way to write this with more of a "RAII"-ish behavior, without having to split acquisition and initialization up?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Darkmeerkat is offline Offline
12 posts
since Feb 2006
Feb 28th, 2007
0

Re: C++ / Exceptions

First, there is nothing wrong with this code.

  1. int main() {
  2. try {
  3. A a_definition;
  4. } catch( Exp error ) {
  5. std::cout << error.message << endl;
  6. }
  7.  
  8. a_definition; // out of scope now...
  9. }

a_definition is indeed out of scope at teh place you mention, but so what ?! You are anyway using it inside the try-catch block (I hope) so you anyway don't it outside the try catch, so let it go out of scope. Now if you really want a code that gives a fake feeling of cleanliness you can do this:


  1. do_that_thing_with_a_definition()
  2. {
  3. A a_definition;
  4. //a_definition is in scope.. a neat and clean function...
  5. }
  6. int main() {
  7. try {
  8. do_that_thing_with_a_definition() ;
  9. } catch( Exp error ) {
  10. std::cout << error.message << endl;
  11. }
  12.  
  13. //following is irrelevant now
  14. //a_definition; // out of scope now...
  15. }
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Feb 28th, 2007
0

Re: C++ / Exceptions

Your second example is even worse because the object will go out of scope as soon as do_that_thing() returns, so any usage of it inside the try ... catch block will now be illegal.

Back on topic:
If you really need to use code outside the constructor, use pointers, although it is a bad design if you are forced to use an object out of its try catch block.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 1st, 2007
0

Re: C++ / Exceptions

Your second example is even worse because the object will go out of scope as soon as do_that_thing() returns, so any usage of it inside the try ... catch block will now be illegal.
May be I wasn't clear. What was meant is that create the object and do whatever you wanna do with the object "inside" do_that_thing_with_a_definition(). I thot the name of the function will make it clear. Anyway..
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 1st, 2007
0

Re: C++ / Exceptions

Click to Expand / Collapse  Quote originally posted by thekashyap ...
May be I wasn't clear. What was meant is that create the object and do whatever you wanna do with the object "inside" do_that_thing_with_a_definition(). I thot the name of the function will make it clear. Anyway..
Well, except that here:
C++ Syntax (Toggle Plain Text)
  1. int main() {
  2. try {
  3. do_that_thing_with_a_definition() ;
  4. } catch( Exp error ) {
  5. std::cout << error.message << endl;
  6. }
  7.  
  8. //following is irrelevant now
  9. //a_definition; // out of scope now...
}

You basically implied that a_definition would be in scope out of the try...catch if you used do_that_thing() function. However, that's not the case. It suffers the same fate as the previous problem, so putting it in a function doesn't really do much good. In fact, you've effectively reduced the scope of the object by allocating it in a function.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 2nd, 2007
0

Re: C++ / Exceptions

Problem stmt is: "It doesn't look very clean; I don't need a pointer". So the solution is such that it is "clean" and "doesn't need a pointer".
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 2nd, 2007
0

Re: C++ / Exceptions

Click to Expand / Collapse  Quote originally posted by thekashyap ...
Problem stmt is: "It doesn't look very clean; I don't need a pointer". So the solution is such that it is "clean" and "doesn't need a pointer".
Clean as it might be, it still doesn't solve the OP's original problem -- which is to be able to use the object outside of the try...catch block.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help
Next Thread in C++ Forum Timeline: Question about private scope





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC