| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Riaan" |
| Date: |
06 Feb 2006 12:35:24 AM |
| Object: |
Class execution |
Hi Everybody,
How do I stop my class of being used after certain init functions failed in
my constructor ?
Be Good
--
Riaan Bekker
.
|
|
| User: "Tomás" |
|
| Title: Re: Class execution |
07 Feb 2006 03:12:31 AM |
|
|
Riaan posted:
Hi Everybody,
How do I stop my class of being used after certain init functions
failed in my constructor ?
Be Good
class Monkey {
public:
class InitError {}; // Just a dummy type
Monkey(int a)
{
if ( !a )
{
throw InitError();
}
}
};
int main()
{
try {
Monkey ape;
}
catch( InitError const k )
{
}
}
Unchecked code, may contain errors/bugs.
-Tomás
.
|
|
|
| User: "Tomás" |
|
| Title: Re: Class execution |
07 Feb 2006 03:16:56 AM |
|
|
catch( InitError const k )
catch ( Monkey::InitError const k)
.
|
|
|
| User: "Ben Pope" |
|
| Title: Re: Class execution |
07 Feb 2006 04:00:24 AM |
|
|
Tomás wrote:
catch( InitError const k )
catch ( Monkey::InitError const k)
catch ( Monkey::InitError const& k)
Ben Pope
--
I'm not just a number. To many, I'm known as a string...
.
|
|
|
|
|
|
| User: "Ian Collins" |
|
| Title: Re: Class execution |
06 Feb 2006 10:01:37 PM |
|
|
Riaan wrote:
Hi Everybody,
How do I stop my class of being used after certain init functions failed in
my constructor ?
Call abort or use an assert. There is no other generic way.
Be Good
I'll try :)
--
Ian Collins.
.
|
|
|
|
| User: "improgrammer" |
|
| Title: Re: Class execution |
06 Feb 2006 11:19:30 PM |
|
|
The generic solution is to throw an exception.
"use an assertion" is one form of throw an exception.
.
|
|
|
| User: "Rein Anders Apeland" |
|
| Title: Re: Class execution |
09 Feb 2006 01:47:03 PM |
|
|
improgrammer wrote:
The generic solution is to throw an exception.
"use an assertion" is one form of throw an exception.
AFAIK 'using an assertion' is NOT a form of 'throwing an exception'.
Exceptions can be caught, assertions cannot. Assertions will abort your
program if the fail.
-Rein
.
|
|
|
|
|

|
Related Articles |
|
|