|
Boost Users : |
Subject: Re: [Boost-users] throw and catch exception
From: Igor R (boost.lists_at_[hidden])
Date: 2011-10-22 15:45:42
> 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 list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net