exceptions: reuse <stdexcept> classes???
Is it a good idea to use classes defined in stdexcept (logic_error, runtime_error... etc). I think it should be.
Basically, I need to throw an exception with a printable message indicating a reason. I was wondering if I should write my own class for it or use logic_error or runtime_error of <stdexcept>.
When thrown exception needs to be customized then it'd be a good idea to write a custom exception class. What do you think?
Also, I'm getting a compile-time error when compiling this code:
Code:
string raison_str( "connect() failed, returned: " );
raison_str += ret_code;
raison_str += "WSAGetLastError() returned: ";
raison_str += rcode;
throw ( logic_error( raison_str ) );
// throw ( logic_error( string( "connect() failed." ) ) );
The compiler (MSVC 6.0) gives following error:
Code:
C:\Ruchikar\RnD\wbsrvc1\Session.cpp(62) : error C2061: syntax error : identifier 'raison_str'
C:\Ruchikar\RnD\wbsrvc1\Session.cpp(62) : error C2066: cast to function type is illegal
C:\Ruchikar\RnD\wbsrvc1\Session.cpp(62) : error C2059: syntax error : ';'
However, when I use the commented line, it works fine. What's wrong here... In both the cases there is a string object.
Thanks in advance.
Re: exceptions: reuse <stdexcept> classes???
Quote:
Originally posted by Ruchikar
Code:
string raison_str( "connect() failed, returned: " );
raison_str += ret_code;
/*
raison_str += "WSAGetLastError() returned: ";
raison_str += rcode;
*/ // comment this all
throw ( logic_error( raison_str ) );
// throw ( logic_error( string( "connect() failed." ) ) );
Have you try it like above.... ???
I think the problem came from the first line not from the line 62 even if the error does show in 62