|
From: Wolf P. <pe...@re...> - 2005-10-06 10:23:57
|
Yes, but you should catch the base exception class by (const-) reference.
for example:
class GeneralX {
public:
// properties ...
virtual bool IsSpecial() const { return false; } //
Standard-Implementation
};
class SpecificX: public GeneralX {
public:
// further properties ...
SpecificX(): m_kind( 815 ) {}
virtual bool IsSpecial() const { return m_kind > 0; } // overrides
Standard-Implementation using new Member
int m_kind;
};
void may_emit(bool x_trigger) {
// do something...
if( x_trigger ) {
throw SpecificX();
}
// do something else...
}
bool test_byref( bool x_trigger) {
bool bSpecificCaught = false;
try {
may_emit(x_trigger);
}
catch( const GeneralX& e ) { // catch by reference
// do something with e...
// if you check against SpecialX you would succeed:
bSpecificCaught = e.IsSpecial();
}
return bSpecificCaught;
}
bool test_byvalue(bool x_trigger) {
bool bSpecificCaught = false;
try {
may_emit(x_trigger);
}
catch( GeneralX e ) { // catch by value
// do something with e...
// if you check against SpecialX you would succeed:
bSpecificCaught = e.IsSpecial();
}
return bSpecificCaught;
}
bool test_all_passed() {
bool by_ref_result = test_byref(true);
bool by_value_result = test_byvalue(true);
return
(by_ref_result == true) &&
(by_value_result == false);
}
This compiles with devcpp test_all_passed() returns true for me.
Wolf Peuker
Noorez Kassam schrieb:
> For my program, I have a main exception class to handle general
> exceptions and all the other exceptions are derived from that one. In
> the catch block, can i catch an error of type "general exception" for
> all the exceptions derived from that class?
>
> _________________________________________________________________
> Designer Mail isn't just fun to send, it's fun to receive. Use special
> stationery, fonts and colors.
> http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines
> Start enjoying all the benefits of MSN® Premium right now and get the
> first two months FREE*.
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by:
> Power Architecture Resource Center: Free content, downloads, discussions,
> and more. http://solutions.newsforge.com/ibmarch.tmpl
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|