Catching exception: divide by zero

后端 未结 8 1631
时光取名叫无心
时光取名叫无心 2020-11-22 16:23

The following code does not catch an exception, when I try to divide by 0. Do I need to throw an exception, or does the computer automatically throw one at runtime?

8条回答
  •  隐瞒了意图╮
    2020-11-22 17:23

    As far as I know C++ specifications does not mention anything about divide by zero exeption. I believe you need to do it yourself...

    Stroustrup says, in "The Design and Evolution of C++" (Addison Wesley, 1994), "low-level events, such as arithmetic overflows and divide by zero, are assumed to be handled by a dedicated lower-level mechanism rather than by exceptions. This enables C++ to match the behaviour of other languages when it comes to arithmetic. It also avoids the problems that occur on heavily pipelined architectures where events such as divide by zero are asynchronous."`

提交回复
热议问题