Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
65 views
in Technique[技术] by (71.8m points)

c++ - No Runtime error after divison by zero

I am using Microsoft visual C++ 2010 and I have the following code in which I divide 1 by zero as you see

#include <cstdio>

int main()
{   
   int x;
   x = 0;
   1/x;

   while (1) {
      std::printf("RUNNING!!
");
   }
}

and surprisingly I don't find any run time error and the program continued to execute and displays RUNNING!!

So my question why "1/x" is not considered a run time error , and why the program don't stop?

question from:https://stackoverflow.com/questions/65861290/can-visual-c-compiler-handle-error-like-related-to-divide-by-zero

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In complement to everybody else, I notice some comments about "getting or not a run-time error" that makes me thinking a misunderstanding of ambiguous terminology.

In common computer science a "runtime error" is no more that what the computer language of human brains (plain English) means: and error that is noticed during "program execution".

So yes, getting a dump from the OS after a signal is a "runtime error". At least for the English language.

But this has noting to do with std::runtime_error that is the std::exception that is thrown by the standard library (or whatever other code based on the standard library) when an error is fund in their own code.

Also the term "exception" is ambiguous: in OS terminology it is the "rescue code" an appropriate OS driver put in answer to a CPU hardware trap. In C++ is is either a base class representing all standard library errors OR whatever value subject of a throw statement.

The point, here, is that an integer division by zero is not a standard library implementation detected error: basic integer arithmetic is considered by the C++ language as primitive to the hosting environment. In most of the platforms, operator/(int,int) is implemented through a DIV assembler instruction (at least on most of the CPU) and a DIV with a 0 operand is handled by the CPU microcode as CPU exception, that produce a "trap" (or interrupt, or whatever the platform terminology calls it) handled by the OS (or a specific OS driver). There is nothing in the C++ compiler (and produced executable) that knows what's going on during the DIV evaluation (since it is internal to the CPU),so there is no throw statement that can be written, and hence no std::exception (or whatever other C++ type) to catch. Just an operating system driver that can be replaced, and that -by default- terminates the application.

That's a very similar behavior of the equivalent default catch(...) in the C++ startup code that invokes main (that calls exit), hence another source of confusion.

To complicate much more things, compiler optimization can discard whatever operation is not producing a used result, making everything to become even not visible.

So, output the division result is a must to let the compiler not discard the operation(s). And when that is done, a CPU trap producing an OS signal is observed. This is -in plain English an "error at run-time", but is not a C++ std::runtime_error since no such a throw statement exist in the division operator implementation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...