在捕获异常时将有用数据转储到控制台

发布于 2024-10-01 06:58:09 字数 1235 浏览 1 评论 0原文

我有一个 CExceptionHandler 类,每当我的应用程序检测到运行时异常时就会调用该类。例如:

if ( val == NULL )
{
    TRACE(_T("Unexpected NULL in sequence."));
    AfxThrowException( ERROR_INVALID_DATA );
}

AfxThrowException非常简单:

void AfxThrowException( DWORD error )
{
    CExceptionHandler * pException = NULL;

    if ( error == 0 )
    {
        error = ::GetLastError();
    }

    pException = new CExceptionHandler( error );

    TRACE(_T("Warning: throwing CSerialException for error %d\n"), error);
    THROW(pException);
}

这是CExceptionHandler的成员Dump函数:

void CExceptionHandler::Dump( CDumpContext & dc ) const
{
    CObject::Dump(dc);

    dc << "m_dwError = " << m_dwError;
}

在我的代码的更高处,我有try/catch 语句:

try
{
    /* My app does stuff where the exception is thrown. */
}
catch( CExceptionHandler * ex )
{
    afxDump << _T("Dumping exceptional data: ") << _T("\r\n");
    ex->Dump( afxDump );
    afxDump << _T("\r\n");
}

我希望将收集的调试信息转储到控制台。但是,当 PC 进入 catch 语句(使用断点验证)时,控制台上没有任何反应。我在调试模式下使用 Visual Studio 2008。想法受到赞赏。谢谢。

I have a CExceptionHandler class that is invoked whenever my application detects a run-time exception. For example:

if ( val == NULL )
{
    TRACE(_T("Unexpected NULL in sequence."));
    AfxThrowException( ERROR_INVALID_DATA );
}

AfxThrowException is very simple:

void AfxThrowException( DWORD error )
{
    CExceptionHandler * pException = NULL;

    if ( error == 0 )
    {
        error = ::GetLastError();
    }

    pException = new CExceptionHandler( error );

    TRACE(_T("Warning: throwing CSerialException for error %d\n"), error);
    THROW(pException);
}

This is the member Dump function of CExceptionHandler:

void CExceptionHandler::Dump( CDumpContext & dc ) const
{
    CObject::Dump(dc);

    dc << "m_dwError = " << m_dwError;
}

Higher up in my code I have the try/catch statements:

try
{
    /* My app does stuff where the exception is thrown. */
}
catch( CExceptionHandler * ex )
{
    afxDump << _T("Dumping exceptional data: ") << _T("\r\n");
    ex->Dump( afxDump );
    afxDump << _T("\r\n");
}

I would like the collected debug information to be dumped to the console. However, when the PC gets into the catch statement (verified with breakpoint), nothing happens on the console. I am using Visual Studio 2008 in Debug mode. Thoughts are appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤千羽 2024-10-08 06:58:09

CDumpContext 将输出发送到调试器,而不是控制台(请参阅 OutputDebugString 了解更多信息),如果您在 Visual Studio 调试器下运行,输出将出现在输出窗口中。

如果您还想将输出发送到控制台,您可以将 CDumpContext 配置为通过将指针传递给 CFile 对象来写入 CFile CDumpContext 构造函数

如果您的应用程序是使用“使用多字节字符集”配置选项构建的,则可以使用 CStdioFile 写入 stdout 或 stderr:

CStdioFile file(stdout);
CDumpContext dumpContext(&file);
ex->Dump(dumpContext);

或者,如果您想使用 afxDump,您可以设置其 m_pFile直接成员变量(声明为public)。例如,您可以将此代码放入您的 main 函数中:

CStdioFile file(stdout);
afxDump.m_pFile = &file;    

但是,如果您的应用程序构建为 Unicode,则这将不起作用,因为字符串需要转换为多字节才能写入标准输出。要进行转换,请编写一个继承 CFile 的类:

class CDumpFile : public CFile
{
    public:
        virtual void Write(const void* lpBuf, UINT nCount);
};

void CDumpFile::Write(const void* lpBuf, UINT nCount)
{
    // Construct string from array of wide chars
    const wchar_t* p = static_cast<const wchar_t*>(lpBuf);
    UINT len = nCount / sizeof(wchar_t);
    CStringW str(p, len);

    CStringA astr(str); // Convert wide char to multibyte

    fputs((LPCSTR)astr, stdout); // Write multibyte string to stdout
}

并按如下方式使用它:

CDumpFile file;
afxDump.m_pFile = &file;

关于代码的其他几点:

  1. 您应该确保在 CFile 中删除异常对象>catch 块。如果您的 CExceptionHandler 类继承了 MFC 的 CException,那么您应该调用 ex->Delete()。如果没有,您需要删除 ex

  2. 我建议不要对自己的函数使用 Afx 前缀 - 在我看来,您应该考虑将其保留供 MFC 库使用。

我希望这有帮助!

CDumpContext sends output to the debugger, not to the console (see OutputDebugString for more information), and if you run under the Visual Studio debugger, the output will appear in the Output window.

If you want to also send output to the console, you can configure CDumpContext to write to a CFile by passing a pointer to a CFile object in the CDumpContext constructor.

If your application is built with the 'Use Multi-Byte Character Set' configuration option, you can use CStdioFile to write to either stdout or stderr:

CStdioFile file(stdout);
CDumpContext dumpContext(&file);
ex->Dump(dumpContext);

Or, if you want to use afxDump, you can set its m_pFile member variable directly (it's declared public). For example you could put this code inside your main function:

CStdioFile file(stdout);
afxDump.m_pFile = &file;    

But, this won't work if your app is built as Unicode because strings need to be converted to multi-byte to be written to stdout. To do the conversion, write a class that inherits CFile:

class CDumpFile : public CFile
{
    public:
        virtual void Write(const void* lpBuf, UINT nCount);
};

void CDumpFile::Write(const void* lpBuf, UINT nCount)
{
    // Construct string from array of wide chars
    const wchar_t* p = static_cast<const wchar_t*>(lpBuf);
    UINT len = nCount / sizeof(wchar_t);
    CStringW str(p, len);

    CStringA astr(str); // Convert wide char to multibyte

    fputs((LPCSTR)astr, stdout); // Write multibyte string to stdout
}

and use it like this:

CDumpFile file;
afxDump.m_pFile = &file;

A couple of other points about your code:

  1. You should ensure that the exception object is deleted in the catch block. If your CExceptionHandler class inherits MFC's CException then you should call ex->Delete(). If it doesn't, you need to delete ex.

  2. I'd advise against using the Afx prefix for own functions - you should consider this reserved for use by the MFC library, in my opinion.

I hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文