| | |
C++ / Exceptions
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2006
Posts: 12
Reputation:
Solved Threads: 0
I'm having some trouble understanding how Exceptions thrown from constructors work...
Let's say I have a class like this:
If I try to define a variable of type "A" within a
Wrapping the whole program in a try/catch block doesn't seem very appealing to me! And while I could do something like this:
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?
Let's say I have a class like this:
C++ Syntax (Toggle Plain Text)
class A { public: A() { if( error ) throw MyExp; } }
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)
int main() { try { A a_definition; } catch( Exp error ) { std::cout << error.message << endl; } a_definition; // out of scope now... }
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)
A *a_ptr = NULL; try { a_ptr = new A; } 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!
keep the constructor simple , do some basic initialization which can not fail, move rest of initialization to another function say init
•
•
Join Date: Feb 2006
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
keep the constructor simple , do some basic initialization which can not fail, move rest of initialization to another function say init
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?
First, there is nothing wrong with this code.
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:
c Syntax (Toggle Plain Text)
int main() { try { A a_definition; } catch( Exp error ) { std::cout << error.message << endl; } a_definition; // out of scope now... }
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:
c Syntax (Toggle Plain Text)
do_that_thing_with_a_definition() { A a_definition; //a_definition is in scope.. a neat and clean function... } int main() { try { do_that_thing_with_a_definition() ; } catch( Exp error ) { std::cout << error.message << endl; } //following is irrelevant now //a_definition; // out of scope now... }
Your second example is even worse because the object will go out of scope as soon as
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.
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.
All my posts may be freely redistributed under the terms of the MIT license.
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..
•
•
•
•
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..
C++ Syntax (Toggle Plain Text)
int main() { try { do_that_thing_with_a_definition() ; } catch( Exp error ) { std::cout << error.message << endl; } //following is irrelevant now //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.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
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".
"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.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- exceptions (Java)
- Exceptions (repost) (C++)
- Exceptions (C++)
- weird exceptions:( (Python)
- How to handle exceptions in constructor? (Java)
- Exceptions (Java)
- Arithmetic Exceptions and exception-handling statements (Java)
Other Threads in the C++ Forum
- Previous Thread: help
- Next Thread: Question about private scope
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






