C++ / Exceptions

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2006
Posts: 12
Reputation: Darkmeerkat is an unknown quantity at this point 
Solved Threads: 0
Darkmeerkat Darkmeerkat is offline Offline
Newbie Poster

C++ / Exceptions

 
0
  #1
Feb 27th, 2007
I'm having some trouble understanding how Exceptions thrown from constructors work...

Let's say I have a class like this:
  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:
  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:
  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!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,836
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 119
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: C++ / Exceptions

 
0
  #2
Feb 27th, 2007
keep the constructor simple , do some basic initialization which can not fail, move rest of initialization to another function say init
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 12
Reputation: Darkmeerkat is an unknown quantity at this point 
Solved Threads: 0
Darkmeerkat Darkmeerkat is offline Offline
Newbie Poster

Re: C++ / Exceptions

 
0
  #3
Feb 27th, 2007
Originally Posted by ithelp View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: C++ / Exceptions

 
0
  #4
Feb 28th, 2007
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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: C++ / Exceptions

 
0
  #5
Feb 28th, 2007
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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: C++ / Exceptions

 
0
  #6
Mar 1st, 2007
Originally Posted by joeprogrammer View Post
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..
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: C++ / Exceptions

 
0
  #7
Mar 1st, 2007
Originally Posted by thekashyap View Post
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:
  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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: C++ / Exceptions

 
0
  #8
Mar 2nd, 2007
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".
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: C++ / Exceptions

 
0
  #9
Mar 2nd, 2007
Originally Posted by thekashyap View Post
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.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC