【问题标题】:How To Catch an Exception [C++/CX]如何捕捉异常 [C++/CX]
【发布时间】:2013-03-29 09:33:53
【问题描述】:

我正在开发一个 Windows 8 应用程序 (C++)。我使用了 Windows 8 示例集合中的 httpclient 类。

inline void CheckHResult(HRESULT hResult)
{
    if (hResult == E_ABORT)
    {
        concurrency::cancel_current_task();
    }
    else if (FAILED(hResult))
    {
        throw Platform::Exception::CreateException(hResult);
    }
}

当应用未连接到互联网时,此函数会引发异常。我正在尝试像这样在以下 lambda 中捕获异常。

return completionTask.then([this, stringCallback](tuple<HRESULT, wstring> resultTuple)
{
    try
    {
        CheckHResult(std::get<0>(resultTuple));
    }

    catch(Exception^ ex)
    {

    }

    return std::get<1>(resultTuple);
}); 

但它仍然显示未处理的异常:

First-chance exception at 0x77194B32 in Sample.exe: Microsoft C++ exception: Platform::COMException ^ at memory location 0x08C7EDF4. HRESULT:0x800C0005
If there is a handler for this exception, the program may be safely continued.

是不是我做错了什么?

【问题讨论】:

    标签: windows-8 exception-handling windows-store-apps c++-cx


    【解决方案1】:

    第一次机会异常并不一定表示您的代码有问题,它与未捕获的异常不同

    This (oldish, but still correct) article 描述了第一次机会异常的真正含义,只是向调试器发出异常已引发的通知,无论以后是否会被捕获。

    在调试应用程序时,只要遇到异常,调试器就会收到通知。此时,应用程序被挂起,调试器决定如何处理异常。 第一次通过这种机制称为“第一次机会”异常。 根据调试器的配置,它将恢复应用程序并传递异常,或者将应用程序挂起并进入调试模式. 如果应用程序处理了异常,它会继续正常运行。

    【讨论】:

    • 非常感谢您的帮助。向像你这样知识渊博的人学习真是太好了:)
    猜你喜欢
    • 2015-12-19
    • 2012-03-20
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 2012-01-10
    • 2013-03-11
    • 2021-10-17
    • 2020-04-20
    相关资源
    最近更新 更多