Ogre对异常的使用是一种很好的使用方式吗?

发布于 2024-08-30 22:17:12 字数 856 浏览 10 评论 0原文

到目前为止,我已经成功地完成了我的 C++ 游戏编程生涯,几乎从未接触过异常,但最近我一直在使用 Ogre 引擎进行一个项目,并且我正在努力正确学习。我在这里找到了很多关于 C++ 异常的一般用法的好问题和答案,但我想从这里获得一些关于 Ogre 的用法是否良好以及如何最好地使用它们的外部意见。

首先,引用 Ogre 的 Exception 类文档:

OGRE 从不使用返回值来指示错误。相反,如果发生错误,则会抛出异常,这是封装问题细节的对象。使用 OGRE 的应用程序应始终确保捕获异常,因此所有 OGRE 引擎函数都应发生在 try{} catch(Ogre::Exception& e) {} 块内。

真的吗?每个 Ogre 函数都可以抛出异常并被包装在 try/catch 块中?目前,我们在使用它时通过 main 中的 try/catch 来处理此问题,退出前将显示一个带有异常描述的消息框。这对于调试来说可能有点尴尬,因为你没有得到堆栈跟踪,只有抛出错误的函数 - 更重要的是我们代码中调用 Ogre 函数的函数。如果它是 Ogre 代码中的断言,那么它将直接进入调试器中的代码,我将能够更轻松地找出发生了什么 - 我不知道我是否遗漏了一些可以让我已经调试异常了吗?

我现在开始在代码中添加更多的 try/catch 块,通常会考虑 Ogre 函数抛出异常是否重要。如果它会停止一切工作,那么让主 try/catch 处理它并退出程序。如果它不是很重要,那么在函数调用之后捕获它并让程序继续。最近的一个例子是为应用于实体的材质构建顶点/片段程序参数的向量 - 如果材质没有任何参数,那么它会抛出异常,我捕获了该异常,然后忽略了它,因为它没有不需要添加到我的参数列表中。这看起来是一种合理的处理事情的方式吗?非常感谢任何关于与 Ogre 合作的具体建议。

I've managed to get through my C++ game programming career so far virtually never touching exceptions but recently I've been working on a project with the Ogre engine and I'm trying to learn properly. I've found a lot of good questions and answers here on the general usage of C++ exceptions but I'd like to get some outside opinions from here on whether Ogre's usage is good and how best to work with them.

To start with, quoting from Ogre's documentation of it's own Exception class:

OGRE never uses return values to indicate errors. Instead, if an error occurs, an exception is thrown, and this is the object that encapsulates the detail of the problem. The application using OGRE should always ensure that the exceptions are caught, so all OGRE engine functions should occur within a try{} catch(Ogre::Exception& e) {} block.

Really? Every single Ogre function could throw an exception and be wrapped in a try/catch block? At present this is handled in our usage of it by a try/catch in main that will show a message box with the exception description before exiting. This can be a bit awkward for debugging though as you don't get a stack trace, just the function that threw the error - more important is the function from our code that called the Ogre function. If it was an assert in Ogre code then it would go straight to the code in the debugger and I'd be able to find out what's going on much easier - I don't know if I'm missing something that would allow me to debug exceptions already?

I'm starting to add a few more try/catch blocks in our code now, generally thinking about whether it matters if the Ogre function throws an exception. If it's something that will stop everything working then let the main try/catch handle it and exit the program. If it's not of great importance then catch it just after the function call and let the program continue. One recent example of this was building up a vector of the vertex/fragment program parameters for materials applied to an entity - if a material didn't have any parameters then it would throw an exception, which I caught and then ignored as it didn't need to add to my list of parameters. Does this seem like a reasonable way of dealing with things? Any specific advice for working with Ogre is much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

べ映画 2024-09-06 22:17:13

您似乎不知道如何调试异常。
要么

  • 调出 VS 调试/异常
    对话框并勾选 C++ 异常
    盒子。这会给你一个
    当出现错误时进行调试的机会(出现一个对话框)
    抛出异常。

或者

  • 如果你已经构建了 Ogre 源,请设置
    Ogre::Exception 中的断点
    构造函数,当它尝试抛出一个时,你会中断
    一个调用堆栈,其中下一级是抛出站点。

You seem to be unaware of how to debug exceptions.
Either

  • Bring up the VS Debug/Exceptions
    dialog and tick the C++ Exceptions
    box. This will give you an
    opportunity (a dialog appears) to debug when an
    exception is thrown.

or

  • If you've built Ogre source, set a
    breakpoint in the Ogre::Exception
    constructor and when it attempts to throw one you'll break with
    a call stack where the next level up is the throw site.
谁人与我共长歌 2024-09-06 22:17:12

您不需要将每次对 Ogre 的最后调用包装在 try { ... } catch 中。只要能有效地处理异常,就可以这样做。在某些情况下,这可能位于单个调用站点,也可能位于某种高级循环中。如果你无法在任何地方有意义地处理它,那就干脆不要抓住它;让调试器接管。

特别是,您不应该因为您引用的原因而在 main() 中捕获异常(至少在开发期间不应该;您应该在生产中)。

You don't need to wrap every last call to Ogre in try { ... } catch. You do it wherever you can meaningfully deal with the exception. This may be at the individual call site in some cases, or it could be in a high-level loop of some sort. If you can't deal with it meaningfully anywhere, don't catch it at all; let the debugger take over.

In particular, you shouldn't catch exceptions in main() for precisely the reason you cite (at least, not during development; you should in production).

策马西风 2024-09-06 22:17:12

恐怕我对 Ogre 一无所知,但异常处理的一般规则是,尽可能从抛出点捕获异常,但不能再进一步了。但是,只有当引发异常的代码使用 RAII 来管理分配的资源时,这才有可能。如果代码使用对普通指针的动态分配或其他形式的手动资源管理,那么您需要在调用站点使用 try 块。如果是这种情况,我会说使用异常是一个坏主意。

I don't know anything about Ogre, I'm afraid, but the general rule with exception handling is that you catch the exception as far as possible from the throw site, but no further. However, this is only possible if the code that throws the exception uses RAII to look after allocated resources. If the code uses dynamic allocation to plain pointers, or other forms of manual resource management, then you need try-blocks at the call site. If this is the case, I'd say use of exceptions is a bad idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文