Oct 18, 2011 at 8:23pm UTC
Hello
im using Code::Blocks 10.05 GNU GCC Compiler
boost 1.44
the problem is that when throw exception my app crash.
here is my code
/---------------------------------------------------------------------------
class trans_error : public std::runtime_error {
public:
explicit trans_error(std::string &s): std::runtime_error(s) {}
};
class Test
{
public:
explicit Test(const std::string &s): m_str(s) {}
~Test(){}
void operator () ()
{
std::string temp;
std::string delim("!");
vector<string> SplitVec;
boost::algorithm::split( SplitVec, m_str, boost::algorithm::is_any_of("!") );
std::string::iterator substart = m_str.begin(), subend;
subend = search(substart, m_str.end(), delim.begin(), delim.end());
temp.insert(temp.begin(), substart, subend);
boost::trim(temp);
if ( temp == "OK" ) {
m_str = "OK";
len = 2;
return;
}
else {
string s = str(format("%s %s") % temp % "is not a valid trans type");
throw trans_error(s);
}
}
private:
string m_str;
string::size_type len;
};
//-----------------------------------------------------------------------------
void socketclientEMVthread( HTTPSocket *_socket)
{
HTTPSocket *_client = _socket;
string t;
*_client >> t;
string trans_in("");
if (_client->isValid()) {
if ( _client->parser.isPostRequest() ) {
trans_in = _client->parser.getPostParams()["trans"];
}
else if ( _client->parser.isGetRequest() ) {
trans_in = _client->parser.getUrlParams()["trans"];
}
else {
cout << str(format("%s") % "incomplete trans at line 198") << endl;
*_client << t;
return;
}
}
string trans_out("");
boost::this_thread::sleep(boost::posix_time::millisec(100));
if (_client->isValid()) {
Test test(trans_in);
try {
test();
}
//want to catch exception thrown in test class
catch(trans_error &e) {
cout << e.what() << endl;
*_client << trans_in;
delete _client;
return;
}
}
*_client << trans_out;
delete _client;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void socketserverEMVthread()
{
try {
HTTPSocketServer in(3080, -1);
while ( true ) {
HTTPSocket *new_sock;
try {
new_sock = in.Accept();
//for every new connection start a thread
}
catch(socket_errors &e) {
cout << e.what() << endl;
}
boost::thread clientThread(
socketclientEMVthread, new_sock);
}
}
catch(socket_errors &e) {
cout << e.what() << endl;
}
}
//-----------------------------------------------------------------------------
int main() {
boost::thread EmvThread(socketserverEMVthread);
try {
EmvThread.join();
}
catch(...) {
}
return 0;
}
appreciate any help
huvcbo.
Last edited on Oct 18, 2011 at 8:24pm UTC
Nov 16, 2011 at 8:41pm UTC
something doesn't seem right with your line:
string s = str(format("%s %s") % temp % "is not a valid trans type");
what is this supposed to do?
I also notice your use of try-catch all in main function - this is generally good practice, but need to just point out that this will not catch any exceptions thrown by the thread-function.
The thread-function needs to catch its own exceptions (which I notice you are doing) as stack unwinding will not propgate through to the thread caller.