你(真的)编写异常安全代码吗? [关闭]

本文翻译自:Do you (really) write exception safe code? [closed]

Exception handling (EH) seems to be the current standard, and by searching the web, I can not find any novel ideas or methods that try to improve or replace it (well, some variations exist, but nothing novel). 异常处理(EH)似乎是当前的标准,并且通过搜索网络,我找不到任何试图改进或替换它的新颖想法或方法(好吧,存在一些变化,但没有新颖的)。

Though most people seem to ignore it or just accept it, EH has some huge drawbacks: exceptions are invisible to the code and it creates many, many possible exit points. 虽然大多数人似乎忽略它或只是接受它,但EH 一些巨大的缺点:代码看不到异常,它会创建许多可能的退出点。 Joel on software wrote an article about it . 乔尔在软件上写了一篇关于它文章 The comparison to goto fits perfect, it made me think again about EH. goto的比较非常完美,它让我再次想到了EH。

I try to avoid EH and just use return values, callbacks or whatever fits the purpose. 我尽量避免使用EH,只使用返回值,回调或任何适合目的的东西。 But when you have to write reliable code, you just can't ignore EH these days : It starts with the new , which may throw an exception, instead of just returning 0 (like in the old days). 但是当你必须编写可靠的代码时,你现在就不能忽视EH :它从new开始,它可能抛出一个异常,而不是只返回0(就像过去一样)。 This makes about any line of C++ code vulnerable to an exception. 这使得任何C ++代码行都容易受到异常的影响。 And then more places in the C++ foundational code throw exceptions... std lib does it, and so on. 然后C ++基础代码中的更多地方抛出异常...... std lib执行它,依此类推。

This feels like walking on shaky grounds .. So, now we are forced to take care about exceptions! 这感觉就像走在摇摇欲坠的地面上 。所以,现在我们被迫关注异常!

But its hard, its really hard. 但它很难,真的很难。 You have to learn to write exception safe code, and even if you have some experience with it, it will still be required to double check any single line of code to be safe! 你必须学会​​编写异常安全代码,即使你有一些经验,它仍然需要仔细检查任何一行代码是安全的! Or you start to put try/catch blocks everywhere, which clutters the code until it reaches a state of unreadability. 或者你开始在任何地方放置try / catch块,这会使代码混乱,直到它达到不可读状态。

EH replaced the old clean deterministical approach (return values..), which had just a few but understandable and easily solveable drawbacks with an approach that creates many possible exit points in your code, and if you start writing code that catches exceptions (what you are forced to do at some point), then it even creates a multitude of paths through your code (code in the catch blocks, think about a server program where you need logging facilities other than std::cerr ..). EH取代了旧的干净确定性方法(返回值..),它只有一些但可以理解且易于解决的缺点,一种方法可以在代码中创建许多可能的退出点,并且如果你开始编写捕获异常的代码(你是什么的)在某些时候被迫做某事),然后它甚至通过你的代码创建了许多路径(catch块中的代码,考虑一个服务器程序,你需要除了std :: cerr之外的日志工具..)。 EH has advantages, but that's not the point. EH有优势,但这不是重点。

My actual questions: 我的实际问题:

  • Do you really write exception safe code? 你真的写异常安全代码吗?
  • Are you sure your last "production ready" code is exception safe? 您确定最后一个“生产就绪”代码是异常安全的吗?
  • Can you even be sure, that it is? 你能确定吗,它是吗?
  • Do you know and/or actually use alternatives that work? 你知道和/或实际使用有效的替代品吗?

#1楼

参考:https://stackoom.com/question/7m71/你-真的-编写异常安全代码吗-关闭


#2楼

Some of us have been using exception for over 20 years. 我们中的一些人已经使用例外超过20年。 PL/I has them, for example. 例如,PL / I有它们。 The premise that they are a new and dangerous technology seems questionable to me. 它们是一种新的危险技术的前提似乎对我来说是个问题。


#3楼

  • Do you really write exception safe code? 你真的写异常安全代码吗?

Well, I certainly intend to. 好吧,我当然打算。

  • Are you sure your last "production ready" code is exception safe? 您确定最后一个“生产就绪”代码是异常安全的吗?

I'm sure that my 24/7 servers built using exceptions run 24/7 and don't leak memory. 我确信使用异常构建的24/7服务器可以全天候运行并且不会泄漏内存。

  • Can you even be sure, that it is? 你能确定吗,它是吗?

It's very difficult to be sure that any code is correct. 很难确定任何代码是否正确。 Typically, one can only go by results 通常,人们只能按结果去做

  • Do you know and/or actually use alternatives that work? 你知道和/或实际使用有效的替代品吗?

No. Using exceptions is cleaner and easier than any of the alternatives I've used over the last 30 years in programming. 没有。使用例外比我在编程过去30年中使用的任何替代方案更清晰,更容易。


#4楼

Leaving aside the confusion between SEH and C++ exceptions, you need to be aware that exceptions can be thrown at any time, and write your code with that in mind. 抛开SEH和C ++异常之间的混淆,您需要意识到可以随时抛出异常,并在编写代码时考虑到这一点。 The need for exception-safety is largely what drives the use of RAII, smart pointers, and other modern C++ techniques. 对异常安全的需求在很大程度上推动了RAII,智能指针和其他现代C ++技术的使用。

If you follow the well-established patterns, writing exception-safe code is not particularly hard, and in fact it's easier than writing code that handles error returns properly in all cases. 如果你遵循完善的模式,编写异常安全的代码并不是特别困难,事实上它比编写在所有情况下都能正确处理错误返回的代码更容易。


#5楼

I really like working with Eclipse and Java though (new to Java), because it throws errors in the editor if you are missing an EH handler. 我非常喜欢使用Eclipse和Java(Java新手),因为如果你缺少一个EH处理程序,它会在编辑器中抛出错误。 That makes things a LOT harder to forget to handle an exception... 这使得忘记处理异常变得更加困难......

Plus, with the IDE tools, it adds the try / catch block or another catch block automatically. 此外,使用IDE工具,它会自动添加try / catch块或其他catch块。


#6楼

Some of us prefer languages like Java which force us to declare all the exceptions thrown by methods, instead of making them invisible as in C++ and C#. 我们中的一些人更喜欢像Java这样的语言,它迫使我们声明方法抛出的所有异常,而不是像C ++和C#那样使它们不可见。

When done properly, exceptions are superior to error return codes, if for no other reason than you don't have to propagate failures up the call chain manually. 如果正确完成,异常优于错误返回代码,如果没有其他原因,您不必手动向上传播失败的调用链。

That being said, low-level API library programming should probably avoid exception handling, and stick to error return codes. 话虽这么说,低级API库编程应该可以避免异常处理,并坚持错误返回代码。

It's been my experience that it's difficult to write clean exception handling code in C++. 根据我的经验,用C ++编写干净的异常处理代码很困难。 I end up using new(nothrow) a lot. 我最终使用了new(nothrow)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章