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

2023-07-01C/C++开发问题
7

本文介绍了获取有关在 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++ 异常的位置的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6