获取有关在 catch 块内抛出 C++ 异常的位置的信息?

Getting information about where c++ exceptions are thrown inside of catch block?(获取有关在 catch 块内抛出 C++ 异常的位置的信息?)

本文介绍了获取有关在 catch 块内抛出 C++ 异常的位置的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C++ 应用程序,它在 try 块中包装了大部分代码.当我捕获异常时,我可以将用户返回到稳定状态,这很好.但我不再收到崩溃转储.我真的很想弄清楚异常发生在代码中的哪个位置,以便我可以记录并修复它.

I've got a c++ app that wraps large parts of code in try blocks. When I catch exceptions I can return the user to a stable state, which is nice. But I'm not longer receiving crash dumps. I'd really like to figure out where in the code the exception is taking place, so I can log it and fix it.

能够在不停止应用程序的情况下获得转储是理想的,但我不确定这是否可能.

Being able to get a dump without halting the application would be ideal, but I'm not sure that's possible.

有什么方法可以找出从 catch 块中抛出异常的位置吗?如果有用,我将在 windows xp 及更高版本上使用本机 msvc++.我的计划是简单地将崩溃记录到不同用户机器上的文件中,然后在崩溃日志达到一定大小后上传.

Is there some way I can figure out where the exception was thrown from within the catch block? If it's useful, I'm using native msvc++ on windows xp and higher. My plan is to simply log the crashes to a file on the various users' machines, and then upload the crashlogs once they get to a certain size.

推荐答案

这可以通过使用 SEH(结构化异常处理)来实现.关键是 MSVC 通过 SEH 实现了 C++ 异常.另一方面,纯 SEH 更强大、更灵活.

This is possible with using SEH (structured exception handling). The point is that MSVC implements C++ exceptions via SEH. On the other hand the pure SEH is much more powerful and flexible.

这是你应该做的.而不是像这样使用纯 C++ try/catch 块:

That's what you should do. Instead of using pure C++ try/catch blocks like this:

try
{
    DoSomething();
} catch(MyExc& exc)
{
    // process the exception
}

你应该用 SEH 块包裹内部代码块 DoSomething:

You should wrap the inner code block DoSomething with the SEH block:

void DoSomething()
{
    __try {
        DoSomethingInner();
    }
    __except (DumpExc(GetExceptionInformation()), EXCEPTION_CONTINUE_SEARCH) {
        // never get there
    }
}

void DumpEx(EXCEPTION_POINTERS* pExc)
{
    // Call MiniDumpWriteDump to produce the needed dump file
}

也就是说,在 C++ try/catch 块中,我们放置了另一个原始 SEH 块,它只转储所有异常而不捕获它们.

That is, inside the C++ try/catch block we place another raw SEH block, which only dumps all the exceptions without catching them.

参见 此处 使用 MiniDumpWriteDump 的示例.

See here for an example of using MiniDumpWriteDump.

这篇关于获取有关在 catch 块内抛出 C++ 异常的位置的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:获取有关在 catch 块内抛出 C++ 异常的位置的信息?

基础教程推荐