Browse Source

改进exception范例代码

tags/v2.9.7
tearshark 6 years ago
parent
commit
75e443cfc1
2 changed files with 22 additions and 7 deletions
  1. 20
    5
      tutorial/test_async_exception.cpp
  2. 2
    2
      vs_proj/librf.cpp

+ 20
- 5
tutorial/test_async_exception.cpp View File

@@ -12,15 +12,14 @@ using namespace resumef;
//请打开结构化异常(/EHa)
auto async_signal_exception(const intptr_t dividend)
{
using namespace std::chrono;
resumef::awaitable_t<int64_t> awaitable;
awaitable_t<int64_t> awaitable;
std::thread([dividend, st = awaitable._state]
{
std::this_thread::sleep_for(50ms);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
try
{
//也可以注释掉这个判断,使用结构化异常。但就获得不了具体描述信息了
if (dividend == 0)
throw std::logic_error("divided by zero");
st->set_value(10000 / dividend);
@@ -34,13 +33,29 @@ auto async_signal_exception(const intptr_t dividend)
return awaitable;
}
auto async_signal_exception2(const intptr_t dividend)
{
awaitable_t<int64_t> awaitable;
std::thread([dividend, st = awaitable._state]
{
std::this_thread::sleep_for(std::chrono::milliseconds(50));
if (dividend == 0)
st->throw_exception(std::logic_error("divided by zero"));
else
st->set_value(10000 / dividend);
}).detach();
return awaitable;
}
future_vt test_signal_exception()
{
for (intptr_t i = 10; i >= 0; --i)
{
try
{
auto r = co_await async_signal_exception(i);
auto r = co_await async_signal_exception2(i);
std::cout << "result is " << r << std::endl;
}
catch (const std::exception& e)

+ 2
- 2
vs_proj/librf.cpp View File

@@ -19,7 +19,7 @@ extern void resumable_main_benchmark_mem();
int main(int argc, const char * argv[])
{
resumable_main_benchmark_mem();
resumable_main_exception();
return 0;
resumable_main_yield_return();
@@ -29,12 +29,12 @@ int main(int argc, const char * argv[])
resumable_main_routine();
resumable_main_resumable();
resumable_main_mutex();
resumable_main_exception();
resumable_main_event();
resumable_main_event_timeout();
resumable_main_dynamic_go();
resumable_main_channel();
resumable_main_cb();
resumable_main_exception();
return 0;
}

Loading…
Cancel
Save