throw and catch exception
I have a code like this when execute the IdHTTPServDataEMVObj.Exequte(); it will throw a std::runtime_error if running one thread all is fine, the catch is excequted, but if running two threads then the application crash. What is the problem, have i miss something appreciate any help. huvcbo.. void server(boost::asio::io_service& io_service, short port) { tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port)); for (;;) { socket_ptr sock(new tcp::socket(io_service)); a.accept(*sock); boost::thread t(boost::bind(socketclientEMVthread, sock, logFunc, mygetFunc, mybackFunc, statusUpdateFunc)); } } the client thread void socketclientEMVthread( socket_ptr sock) { cout << "connect on port " << sock->remote_endpoint().port() << endl; for (;;) { char data[max_length]; boost::system::error_code error; size_t length = sock->read_some(boost::asio::buffer(data), error); if (error == boost::asio::error::eof) break; // Connection closed cleanly by peer. else if (error) throw boost::system::system_error(error); // Some other error. std::string trans_in(data), trans_out; CIdHTTPServDataEMVObj IdHTTPServDataEMVObj(trans_in); try { // this is thrown IdHTTPServDataEMVObj.Exequte(); if ( IdHTTPServDataEMVObj.txBufferlen > 0 && IdHTTPServDataEMVObj.txBufferlen != std::string::npos) { trans_out = IdHTTPServDataEMVObj.txBuffer; } else { trans_out = trans_in; } } catch(trans_error &te) { trans_out = te.what(); boost::asio::write(*sock, boost::asio::buffer(trans_out, trans_out.size())); break; } boost::asio::write(*sock, boost::asio::buffer(trans_out, trans_out.size())); break; } }
throw and catch exceptionI dont get any response on this so i will ask again
i start a new thread,
the thread instance class Foo,
Foo foo;
try {
foo.exequte();
//foo.execute throw a std::exception
}
catch(std::exception) {
}
if start one thread i catch the std::exception,
but if i run several threads the app crash.
i start the thread like this
boost::thread t(boost::bind(newthread));
appreciate any help.
//Christer
"Christer Borgqvist"
i start a new thread, the thread instance class Foo, Foo foo; try { foo.exequte(); //foo.execute throw a std::exception } catch(std::exception) {
} if start one thread i catch the std::exception,
but if i run several threads the app crash. i start the thread like this boost::thread t(boost::bind(newthread));
If the thread function newthread() in the above code does not catch *all* the exceptions, then an uncaught exception would terminate your program. Just debug your application to see what exception is thrown and where.
I have a code like this
when execute the IdHTTPServDataEMVObj.Exequte(); it will throw a std::runtime_error if running one thread all is fine, the catch is excequted, but if running two threads then the application crash.
By saying "one thread" you mean "main thread only"? If so, you probably do catch some exception in the main thread, but don't catch it in the thread-function. <...>
void server(boost::asio::io_service& io_service, short port) { tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port)); for (;;) { socket_ptr sock(new tcp::socket(io_service)); a.accept(*sock); boost::thread t(boost::bind(socketclientEMVthread, sock, logFunc, mygetFunc, mybackFunc, statusUpdateFunc)); } }
FWIW, note that the the above code might throw exceptions...
void socketclientEMVthread( socket_ptr sock)
{ cout << "connect on port " << sock->remote_endpoint().port() << endl;
for (;;) { char data[max_length];
boost::system::error_code error; size_t length = sock->read_some(boost::asio::buffer(data), error); if (error == boost::asio::error::eof) break; // Connection closed cleanly by peer. else if (error) throw boost::system::system_error(error); // Some other error.
It seems that you never catch the above exception.
std::string trans_in(data), trans_out;
CIdHTTPServDataEMVObj IdHTTPServDataEMVObj(trans_in); try { // this is thrown IdHTTPServDataEMVObj.Exequte(); if ( IdHTTPServDataEMVObj.txBufferlen > 0 && IdHTTPServDataEMVObj.txBufferlen != std::string::npos) { trans_out = IdHTTPServDataEMVObj.txBuffer; } else { trans_out = trans_in; } } catch(trans_error &te) { trans_out = te.what(); boost::asio::write(*sock, boost::asio::buffer(trans_out, trans_out.size())); break; }
Catch all the exceptions here - even just for debugging purposes.
boost::asio::write(*sock, boost::asio::buffer(trans_out, trans_out.size())); break;
The above code might throw as well. HTH, Igor'.
ok
if i have a catch(...) {
}
it is the same
//Christer
"Igor R"
i start a new thread, the thread instance class Foo, Foo foo; try { foo.exequte(); //foo.execute throw a std::exception } catch(std::exception) {
} if start one thread i catch the std::exception,
but if i run several threads the app crash. i start the thread like this boost::thread t(boost::bind(newthread));
If the thread function newthread() in the above code does not catch *all* the exceptions, then an uncaught exception would terminate your program. Just debug your application to see what exception is thrown and where.
I have a code like this
when execute the IdHTTPServDataEMVObj.Exequte(); it will throw a std::runtime_error if running one thread all is fine, the catch is excequted, but if running two threads then the application crash.
By saying "one thread" you mean "main thread only"? If so, you probably do catch some exception in the main thread, but don't catch it in the thread-function.
<...>
void server(boost::asio::io_service& io_service, short port) { tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port)); for (;;) { socket_ptr sock(new tcp::socket(io_service)); a.accept(*sock); boost::thread t(boost::bind(socketclientEMVthread, sock, logFunc, mygetFunc, mybackFunc, statusUpdateFunc)); } }
FWIW, note that the the above code might throw exceptions...
void socketclientEMVthread( socket_ptr sock)
{ cout << "connect on port " << sock->remote_endpoint().port() << endl;
for (;;) { char data[max_length];
boost::system::error_code error; size_t length = sock->read_some(boost::asio::buffer(data), error); if (error == boost::asio::error::eof) break; // Connection closed cleanly by peer. else if (error) throw boost::system::system_error(error); // Some other error.
It seems that you never catch the above exception.
std::string trans_in(data), trans_out;
CIdHTTPServDataEMVObj IdHTTPServDataEMVObj(trans_in); try { // this is thrown IdHTTPServDataEMVObj.Exequte(); if ( IdHTTPServDataEMVObj.txBufferlen > 0 && IdHTTPServDataEMVObj.txBufferlen != std::string::npos) { trans_out = IdHTTPServDataEMVObj.txBuffer; } else { trans_out = trans_in; } } catch(trans_error &te) { trans_out = te.what(); boost::asio::write(*sock, boost::asio::buffer(trans_out, trans_out.size())); break; }
Catch all the exceptions here - even just for debugging purposes.
boost::asio::write(*sock, boost::asio::buffer(trans_out, trans_out.size())); break;
The above code might throw as well.
HTH,
Igor'. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
throw and catch exceptionwhen i run gdb i get this
Program received signal SIGSEGV, Segmentation fault.
[Switching to thread 380.0xcc]
_Unwind_SjLj_RaiseException (exc=0x37e38d8) at ../../../../gcc-4.4.1/libgcc/../gcc/unwind-sjlj.c:297
297 ../../../../gcc-4.4.1/libgcc/../gcc/unwind-sjlj.c: No such file or directory.
in ../../../../gcc-4.4.1/libgcc/../gcc/unwind-sjlj.c
Current language: auto; currently c
when throw
/Christer
"Christer Borgqvist"
On Tue, Oct 25, 2011 at 03:52, Christer Borgqvist
"Christer Borgqvist"
skrev i meddelandet news:000001cc8ef1$fac46820$c500a8c0@christer618c56... I have a code like this
when execute the IdHTTPServDataEMVObj.Exequte(); it will throw a std::runtime_error if running one thread all is fine, the catch is excequted, but if running two threads then the application crash. What is the problem, have i miss something
Your code doesn't look thread-safe. Try placing locks around variables that might be shared. Be careful in avoiding deadlock, though.
participants (3)
-
Andrey Moshbear
-
Christer Borgqvist
-
Igor R