是C++预计将在Main()中处理例外?
我正在C ++做一个项目,而我是OOP的新手。
我对C ++例外以及必须处理的地方有一些疑问。 我读到,将try/Catch
块插入main()
函数的好习惯是一种很好的做法main()
并在此处处理,在这种情况下对用户反馈进行编程。
问题在于,并非所有项目的错误都被视为例外,因此,在main()
中称为高级函数返回一个BOOL值,该值是错误状态的第一个指标。
因此,我想知道不处理main()
中的异常是一件好事false
在高水平的值中捕获异常时,使我能够以相同的方式对待所有错误。
I am doing a project in C++, and I am new to OOP.
I have some doubts regarding C++ exceptions and where they have to be handled.
I read that it is a good practice to insert the try/catch
block into the main()
function, letting the exceptions thrown into "deep placed" methods to climb up to main()
and be handled there, programming the user feedback in such cases.
The problem is that not all the errors of the project are treated like exceptions, and for that reason high level functions called in main()
return a bool value that is a first indicator of an error state.
So, I am wondering whether it is a good thing not to handle exceptions in main()
in that case, placing the try/catch
block into low-level methods and returning a false
value in the high-level one when an exception has been caught in the former, allowing me to treat all the errors the same way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有一般规则这些处理程序应该去。
但是,您将您的程序描述为“
main()
返回bool值的高级功能”。这是一个合理的选择:这些高级功能都会照顾任何特定的例外处理。从main
的角度来看,这些高级功能要么成功或失败。因此,main
在您的特定设计中无需打扰异常。There is no general rule where these handlers should go.
But you describe your program as "high level functions called in
main()
return a bool value". That is a reasonable choice: any specific exception handling is taken care of by those high-level functions. From the perspective ofmain
, these high-level functions either succeed or they fail. Hence,main
does not need to bother with exceptions in your specific design.