问题描述
我安装的CLion可能是默认配置。我认为这有问题,因为我看不到例外情况。例如,这段代码:
I have CLion installed with presumably default configuration. I think something is wrong with it, because I can't see exceptions. For example, this code:
int main(){ throw 5; }
仅打印进程退出代码为0
- 为什么不打印异常?
- 为什么打印0而不是1?
作比较:
int main(){try { throw 5; } catch(int x) { std::cout << x << '\n'; }}
这将显示5,因此看起来代码已正确运行,并且异常已正确抛出。
This prints 5, so it looks like code is correctly run and the exception is correctly thrown. It's just hidden by CLion somehow.
编辑:这与没有看到任何控制台输出重复。我的问题非常清楚,我确实看到了打印的控制台输出。我的问题专门针对异常,而不是控制台输出。
推荐答案
在您的第一段代码中,您没有发现异常,因此由默认处理程序处理。因此,您无法控制可执行文件的返回代码。
In your first piece of code you have not caught the exception, so it is handled by the default handler. You therefore have no control over the return code from the executable. Operation is as expected.
如果希望CLion显示异常,则可以对其进行配置。请注意,这仅在CLion调试可执行文件时适用,在CLion之外,可执行文件将继续按照您已经看到的方式运行。
If you would like CLion to display the exception you can configure it to do so. Note that this will only apply when CLion is debugging your executable, outside of CLion your executable will continue to behave as you have already seen.
- 转到运行,然后查看断点。
- 转到异常断点,并在引发任何异常时 >。
- 要在引发异常时显示堆栈跟踪,请检查堆栈跟踪
- 如果您想要程序要停止干预,请检查已启用和抛出时。
- 确保使用运行/调试 >而不是运行/运行来启动程序。
- Go to run then view breakpoints.
- Go to exception breakpoints and when any is thrown.
- To display the stack trace when the exception is thrown check stack trace
- If you would like your program to stop for your intervention, check enabled and when thrown.
- Make sure to use Run/Debug and not Run/Run to launch your program.
这篇关于如何让CLion显示异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!