【问题标题】:C++ MSVC - Show uncaught exception messageC++ MSVC - 显示未捕获的异常消息
【发布时间】:2019-11-14 07:04:51
【问题描述】:

我有一个简单的 C++ 代码,我尝试使用 Visual Studio 2019 进行编译:

#include <iostream>
#include <stdexcept>

int main()
{
    std::cout << "Hello World!\n";
    throw std::runtime_error{ "TEST" };
    return 0;
}

PowerShell 中运行此程序只会显示“Hello world”。根据我使用 GCC 的经验,我希望在终端输出中的某处看到包含 TEST 的消息,例如:

Hello World!                                                                                                                                    
terminate called after throwing an instance of 'std::runtime_error'                                                                             
  what():  TEST                                                                                                                                 
Aborted 

这些是使用的编译器标志:

/permissive- /GS /GL /analyze- /W3 /Gy /Zc:wchar_t /Zi /Gm- /O2 /sdl /Fd"Release\vc142.pdb" /Zc:inline /fp:precise /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /Gd /Oy- /Oi /MD /std:c++17 /FC /Fa"Release\" /EHsc /nologo /Fo"Release\" /Fp"Release\ThrowTest.pch" /diagnostics:column 

如何在 Windows 上使用 MSVC 编译这个程序,以显示运行时抛出的异常?

【问题讨论】:

标签: c++ exception visual-c++ compilation


【解决方案1】:

如何在 Windows 上使用 MSVC 编译这个程序,以显示运行时抛出的异常?

如果你想在编译后看到消息,你需要try catch块,你需要捕获异常:

int runTimeError(){
    std::cout << "Hello World!\n";
    try{
        throw std::runtime_error( "TEST" );
    }catch (std::exception& ex){
        std::cout << ex.what();
    }
    return 0;
}

【讨论】:

  • To be able to compile using MSVC you need to change the {} to (), or curly to round brackets这是不正确的
猜你喜欢
  • 1970-01-01
  • 2012-10-19
  • 2014-08-08
  • 1970-01-01
  • 2013-02-01
  • 2011-06-09
  • 1970-01-01
  • 1970-01-01
  • 2015-01-04
相关资源
最近更新 更多