Board index » cppbuilder » Exception specification
Ettienne Gilbert
![]() CBuilder Developer |
Exception specification2007-03-28 12:33:10 AM cppbuilder95 The following (rather contrived) code snippet generates a Compiler warning ([C++ Warning] Unit1.cpp(56): W8078 Throw expression violates exception specification.), which is obviously wrong. class MyException : public Exception { public: MyException(char* s = "Unknown") : Exception(s){} MyException(AnsiString s = "Unknown") : Exception(s){} MyException(const MyException& e ): Exception(e.Message){} char* msg() const { return Message.c_str(); } private: }; class AnotherException : public Exception { public: AnotherException(char* s = "Another") : Exception(s){} AnotherException(AnsiString s = "Another") : Exception(s){} AnotherException(const AnotherException& e ): Exception(e.Message){} char* msg() const { return Message.c_str(); } private: }; void __fastcall ThrowIt() throw(MyException); #pragma argsused int main(int argc, char* argv[]) { try { ThrowIt(); return 0; } catch( Exception & e) { MessageDlg(e.Message, mtError, TMsgDlgButtons() << mbOK, 0); return -1; } } //--------------------------------------------------------------------------- void __fastcall ThrowIt() throw(MyException) { int *x = NULL; try { try { x = new int[10]; } catch(...) { OutOfMemoryError(); } throw AnotherException("-Another- happened here"); delete []x; //Unreachable for exercise... } catch( Exception& e) { if(x) delete []x; AnsiString s = e.Message; s += "+ some additional info"; throw MyException(s); } } //--------------------------------------------------------------------------- This warning is probably the result of the compiler only tracking 'throw' expressions, rather than 'throw' expressions in conjunction with relevant 'catch' expressions. I have disabled the warning in my projects - so it does not really bother me - but is this a candidate for QC? Regards, Ettienne |