[問題] How to us C++ Exception in Asynchronous I/O
我在用 boost 的 asio 來處理網路的 message
我用的是非同步模式 pseudo code 如下
void handle_read_body(error_code& error)
{
int result = ntohs(ack->Result); // 得到網路回傳結果
//if( result != 0)
throw(error_ack_code("SendLogClient: Ack fail")); // force to throw
}
int main()
{
// initial io_service object
try{
socket_.async_write(endpoint,handle_read_body,..));
sleep(100); // wait for catch happend
}catch(...){
// catch exception
}
}
我對於 C++ exception 的了解,exception 會 rewind 堆疊,
而對於非同步的 I/O,main 所在的堆疊跟 handle_read_body 所在的堆疊
不一樣。 因此若是 handle_read_body 發生例外,則程式會直接跳出,
main 捕捉不到。
我想請問的是,若是用非同步I/O的寫法來處理的話,能夠用 C++ 例外處理的方式嗎
怎樣才能夠捕捉
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.134.96.34
→
12/16 22:04, , 1F
12/16 22:04, 1F
→
12/16 22:04, , 2F
12/16 22:04, 2F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章