简体   繁体   English

Visual Studio 2017中未捕获的C ++异常

[英]c++ exceptions not caught in visual studio 2017

I can't find a way to actually enable c++ exception handling in visual studio 2017. Maybe I am missing something trivial. 我找不到在Visual Studio 2017中实际启用c ++异常处理的方法。也许我缺少一些琐碎的东西。 I've searched a lot and found nothing that solves this simple issue. 我已经搜索了很多东西,却找不到解决这个简单问题的方法。

Even this simple code does not act as expected: 即使这个简单的代码也无法按预期运行:

#include <iostream>
//#include <exception>

int main(int argc, char *argv[])
{
  try
  {
    int j = 0;
    int i = 5 / j;

    std::cout << "i value = " << i << "\n";

    std::cout << "this line was actually reached\n";
  }
  catch (...)
  //catch (std::exception e)
  {
    std::cout << "Exception caught!\n";
    return -1;
  }

  std::cout << "Exception was NOT caught!\n";
  std::cin.get();
}

When I execute this simple code the program crashes, like the exception is never caught, I tried also with std::exception variant, and i get the same result. 当我执行此简单代码时,程序崩溃,就像从未捕获到异常一样,我也尝试使用std :: exception变体,并且得到相同的结果。

The division by zero is just an example, I could have wrote also something like this: 除以零只是一个例子,我也可以这样写:

ExampleClass* pClassPointer = null;
pClassPointer->doSomething();

and expected that a nullpointer exception was automatically thrown and captured by the catch(...) clause. 并预期catch(...)子句会自动引发并捕获nullpointer异常。

Just as an information, I added the line: 作为信息,我添加了以下行:

std::cout << "i value = " << i << "\n";"

otherwise the compiler optimization would have skipped most of the code and the result (in RELEASE mode) was: 否则,编译器优化将跳过大多数代码,并且结果(在RELEASE模式下)为:

this line was actually reached
Exception was NOT caught!

In the properties page of the project, in the option "Enable C++ Exceptions" I have the value: "Yes (/EHsc)", so I think exceptions should be actually enabled. 在项目的属性页中,在“启用C ++异常”选项中,我具有值:“是(/ EHsc)”,因此我认为应该实际启用异常。

Am I missing something? 我想念什么吗? Thank you in advance. 先感谢您。 Andrea 安德里亚

---- Edit ----编辑

I actually just found out that changing the option "Enable C++ Exceptions" with the value "Yes with SEH Exceptions (/EHa)" instead of "Yes (/EHsc)" allows to capture access violation and divide by zero errors in the catch(...) clause. 实际上,我发现将选项“ Enable C ++ Exceptions”更改为“ Yes with SEH Exceptions(/ EHa)”而不是“ Yes(/ EHsc)”可以捕获访问冲突并在catch中将错误除以零( ...)子句。 I've also found very useful information about this subject and whether or not is a good idea to catch those type of exceptions in the replies to this other thread: Catching access violation exceptions? 我还发现了有关此主题的非常有用的信息,并且在对其他线程的答复中是否可以捕获这些类型的异常是一个好主意: 捕获访问冲突异常?

In C++ you throw an exception, and, having done that, you can catch it. 在C ++中,您throw一个异常,然后,您可以catch该异常。 If you didn't throw it, you can't catch it. 如果不扔,就抓不到。

Confusingly, there are many kinds of errors that are generically referred to as "exceptions". 令人困惑的是,有许多错误通常被称为“异常”。 This is especially true in floating-point math, where anything that goes wrong is a "floating-point exception". 在浮点数学中尤其如此,其中任何出错的地方都是“浮点异常”。 It's the same word, but those exceptions are not C++ exceptions, and you cannot reliably catch them. 含义相同,但是这些异常不是C ++异常,您无法可靠地捕获它们。

In general, when something goes wrong in your program you get undefined behavior, that is, the language definition does not tell you what the program does. 通常,当程序中出现问题时,您会得到未定义的行为,也就是说,语言定义不会告诉您程序做什么。 So you're on your own: if your compiler documents what it does when a program divides an integer value by 0, then you can rely on that behavior. 因此,您是一个人:如果编译器记录了程序将整数值除以0时的工作方式,那么您可以依靠该行为。 If not, don't count on anything. 如果没有,请不要指望任何东西。

#include <iostream>


int main(int argc, char *argv[])
{
  try
  {
    int j = 0;
    int i ;
    if(j==0)
      throw j; // you have  throw some variable or atleast use throw for your 
               //catch to work

    std::cout << "i value = " << j/5<< "\n";

    std::cout << "this line was actually reached\n";
  }

  catch (int e) /*catch will work only if the data type of argument matches with the thrown variable's data type if it does not then the compiler's default catch will run*/
  {
    std::cout << "Exception caught!\n";
    return -1;
  }

  std::cout << "Exception was NOT caught!\n";
  std::cin.get();
}

if u still have any questions ask in comments! 如果您还有任何疑问,请在评论中提问!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM