c++ - "break when an exception is void"是什么意思?

标签 c++ visual-studio-2010 visual-c++ exception

过去我发现 Visual C++ 2010 的自动中断异常功能非常有用,今天我查看了此对话框中的选项,发现其中一种异常类型是“void”。这是什么意思?如果我选择这个,我会在代码中抛出的任何异常上中断吗?如果不是,什么样的 throw 语句会触发这种类型的断点?

Visual Studio Exceptions Dialog

我想一个更一般的后续问题是我在哪里可以找到有关此对话框及其所有选项的文档?

最佳答案

关于它是如何工作的(正如 MSalters 所提到的),这只是不正确的命名。

其实应该命名为void*,当然。

但为什么它对 int*const char* 等有效(实际上是任何指针类型,包括指向用户定义类型的指针) ?

好吧,我可以假设这与非常有趣的 C++ 异常处理问题有关 - catch(void*) 异常处理程序实际上捕获任何(与 cv 兼容的)指针类型异常!

例子:

try
{
   throw "Catch me if you can!";      
}
catch(void* e)
{
   // ...and I can!
   std::cout << "Gotcha!" << std::endl;
}

这里我们抛出 char*(在 Visual C++ 中,char 文字是 char*,而不是 const char*)并被 无效*。它会起作用的!

答案可以在C++神圣标准中找到:

§15.3 Handling an exception

15.3.3 A handler is a match for an exception object of type E if

...

the handler is of type cv1 T* cv2 and E is a pointer type that can be converted to the type of the handler by either or both of — a standard pointer conversion (4.10) not involving conversions to pointers to private or protected or ambiguous classes

...

并且4.10说标准指针转换包括转换为void*:

4.10.2 A prvalue of type “pointer to cv T,” where T is an object type, can be converted to a prvalue of type “pointer to cv void”.

另请注意,Visual Studio 调试器的工作方式类似,但不完全是这种方式。不同之处在于它忽略了 cv 限定符。所以 Exceptions 对话框中的 void 实际上意味着任何 [any cv] void*。捕获处理程序将不会忽略它们:

try
{
   struct Throwee {};
   const Throwee* t = nullptr;
   throw t;      
}
catch(void* e)
{
   // Will be missed
   std::cout << "Gotcha!" << std::endl;
}
catch(const void* e)
{
   // Will be catched
   std::cout << "Gotcha const!" << std::endl;
}

关于c++ - "break when an exception is void"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14526942/

相关文章:

visual-studio-2010 - 使用Jenkins/Hudson部署.NET

multithreading - 多线程场景中的 Microsoft.ACE.OLEDB.12.0 错误

c# - 如何通过按快捷键组合来检查是否调用了 Visual Studio 插件?

visual-studio-2010 - WebsiteSpark和Visual Studio 2010

c++ - cl.exe发行或调试

c++ - 使用vs2015社区encog-c编译报错

c++ - 关于创建新对象的运行时错误

c++ - std::set 中包含 std::map 的迭代器

c++ - 数组大小错误 x64 进程

c++ - 为什么值(value)不匹配?