簡體   English   中英

使用std :: runtime_error而不是CException

[英]Using std::runtime_error instead of CException

當我在主線程中拋出CException時,框架會很好地捕獲它,並且一個不錯的MessageBox會顯示錯誤文本。 當我拋出std :: runtime_error時,應用程序崩潰了。 問題是我看不到異常的文字,我必須花時間弄清楚它實際上是我“拋出”的東西,而不僅僅是訪問沖突。

所以我想知道是否有一種方法可以捕獲std :: exception並以類似於CException的方式顯示其文本。

我希望能夠從任何消息處理程序中引發std :: runtime_error而不會導致程序崩潰,而無需在try ... catch中包裝每個消息處理程序。 對於CException,這已經是可能的,因為在事件泵的代碼中有一個try ... catch(我認為是CWinApp :: Run-但我不確定)。

[編輯]我發現了捕獲CExceptions的函數,但是我不確定是否可以覆蓋它。 我已經在下面發布了代碼。 TRY ... CATCH_ALL ... END_CATCH_ALL語句正在捕獲CException。

/////////////////////////////////////////////////////////////////////////////
// Official way to send message to a CWnd

LRESULT AFXAPI AfxCallWndProc(CWnd* pWnd, HWND hWnd, UINT nMsg,
    WPARAM wParam = 0, LPARAM lParam = 0)
{
    _AFX_THREAD_STATE* pThreadState = _afxThreadState.GetData();
    MSG oldState = pThreadState->m_lastSentMsg;   // save for nesting
    pThreadState->m_lastSentMsg.hwnd = hWnd;
    pThreadState->m_lastSentMsg.message = nMsg;
    pThreadState->m_lastSentMsg.wParam = wParam;
    pThreadState->m_lastSentMsg.lParam = lParam;

#ifdef _DEBUG
    _AfxTraceMsg(_T("WndProc"), &pThreadState->m_lastSentMsg);
#endif

    // Catch exceptions thrown outside the scope of a callback
    // in debug builds and warn the user.
    LRESULT lResult;
    TRY
    {
#ifndef _AFX_NO_OCC_SUPPORT
        // special case for WM_DESTROY
        if ((nMsg == WM_DESTROY) && (pWnd->m_pCtrlCont != NULL))
            pWnd->m_pCtrlCont->OnUIActivate(NULL);              
#endif

        // special case for WM_INITDIALOG
        CRect rectOld;
        DWORD dwStyle = 0;
        if (nMsg == WM_INITDIALOG)
            _AfxPreInitDialog(pWnd, &rectOld, &dwStyle);

        // delegate to object's WindowProc
        lResult = pWnd->WindowProc(nMsg, wParam, lParam);

        // more special case for WM_INITDIALOG
        if (nMsg == WM_INITDIALOG)
            _AfxPostInitDialog(pWnd, rectOld, dwStyle);
    }
    CATCH_ALL(e)
    {
        lResult = AfxProcessWndProcException(e, &pThreadState->m_lastSentMsg);
        TRACE(traceAppMsg, 0, "Warning: Uncaught exception in WindowProc (returning %ld).\n",
            lResult);
        DELETE_EXCEPTION(e);
    }
    END_CATCH_ALL

    pThreadState->m_lastSentMsg = oldState;
    return lResult;
}

所以我想知道是否有一種方法可以捕獲std :: exception並以類似於CException的方式顯示其文本。

是的-通過捕獲它,並通過MessageBox顯示它的文本。

int main() {
    try {
        //....
    }
    catch(const std::exception& except) {
        MessageBox(NULL, except.what(), "OMGWTF FATAL ERROR", MB_OK);
    }
}

有,但是您必須自己做。 :-)

int main()
{
    try
    {
        SomeFunction();
    }
    catch (const std::exception & ex)
    {
        ::MessageBox(0, ex.what(), 0, 0);
    }
}

在MFC的主消息循環實現中的某個地方,它具有try / catch設置,該設置提供了拋出CException類型時看到的行為。

您可以將自己的代碼包裝在各種try / catch語句中,以捕獲異常,正如其他人已經指出的那樣。

也可以用一種“頂級”處理程序包裝MFC的消息循環,以捕獲其他未捕獲的內容。 為此,請在派生的應用程序類中重寫CWinApp::Run ,實現所需的try / catch,然后從try塊中調用基本的CWinApp::Run

int CMyApp::Run()
{
    try
    {
        return CWinApp::Run();
    }
    catch(const std::exception& ex)
    {
        MessageBox(NULL, ex.what(), "Error", MB_OK | MB_ICONERROR);
        return 1;  // or some appropriate code
    }
}

如果您有可以合理地繼續執行的異常,則還可以替代CWinApp :: Run()或在CWinApp :: Run()之外重寫CWinApp :: PumpMessage()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM