CLion为什么不显示异常?

问题描述 投票:1回答:1

CLion在运行代码时似乎没有向我显示任何异常。为了测试这一点,我仅使用以下代码创建了一个新项目:

#include <iostream>

int main() {
    std::cout << "--- One" << std::endl;
    throw 6;
    std::cout << "--- Two" << std::endl;
    return 0;
}

这将导致以下输出:

C:\Users\david\CLionProjects\untitled\cmake-build-debug\untitled.exe
--- One

Process finished with exit code 0

如您所见,执行该异常之前的代码,并且不执行该异常之后的代码(正如您期望的那样)。但是,它没有显示有关异常的消息,而是显示“进程以退出代码0结束”,好像没有发生异常。

在Ubuntu上(通过终端)编译和执行的相同代码显示错误消息。因此,我认为问题出在CLion。

我如何解决此问题,以便可以在代码中看到异常消息?

是否有任何设置可能导致这种行为?

我正在Windows 10和Cygwin上使用CLion。这是问题的屏幕截图:

Here's a screenshot of the problem

c++ cmake cygwin clion jetbrains-ide
1个回答
0
投票

投掷也需要尝试抓住

发件人:http://www.cplusplus.com/doc/tutorial/exceptions/

// exceptions
#include <iostream>
using namespace std;

int main () {
  try
  {
    throw 20;
  }
  catch (int e)
  {
    cout << "An exception occurred. Exception Nr. " << e << '\n';
  }
  return 0;
}

在cygwin下编译并运行的程序显示:

$ g++ prova1.cc -o prova1

$ ./prova1
An exception occurred. Exception Nr. 2
© www.soinside.com 2019 - 2024. All rights reserved.