在捕获异常时将有用数据转储到控制台
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CDumpContext
将输出发送到调试器,而不是控制台(请参阅OutputDebugString
了解更多信息),如果您在 Visual Studio 调试器下运行,输出将出现在输出窗口中。如果您还想将输出发送到控制台,您可以将
CDumpContext
配置为通过将指针传递给CFile
对象来写入CFile
CDumpContext
构造函数。如果您的应用程序是使用“使用多字节字符集”配置选项构建的,则可以使用
CStdioFile
写入 stdout 或 stderr:或者,如果您想使用
afxDump
,您可以设置其m_pFile
直接成员变量(声明为public
)。例如,您可以将此代码放入您的main
函数中:但是,如果您的应用程序构建为 Unicode,则这将不起作用,因为字符串需要转换为多字节才能写入标准输出。要进行转换,请编写一个继承
CFile
的类:并按如下方式使用它:
关于代码的其他几点:
您应该确保在
CFile 中删除异常对象>catch
块。如果您的CExceptionHandler
类继承了 MFC 的CException
,那么您应该调用ex->Delete()
。如果没有,您需要删除 ex
。我建议不要对自己的函数使用
Afx
前缀 - 在我看来,您应该考虑将其保留供 MFC 库使用。我希望这有帮助!
CDumpContext
sends output to the debugger, not to the console (seeOutputDebugString
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 aCFile
by passing a pointer to aCFile
object in theCDumpContext
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:Or, if you want to use
afxDump
, you can set itsm_pFile
member variable directly (it's declaredpublic
). For example you could put this code inside yourmain
function: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
:and use it like this:
A couple of other points about your code:
You should ensure that the exception object is deleted in the
catch
block. If yourCExceptionHandler
class inherits MFC'sCException
then you should callex->Delete()
. If it doesn't, you need todelete ex
.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!