Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a trap, an error, a failure and program abortion? [closed]

I see often the following terms in C++ interview questions :

  • program abort
  • error
  • failure
  • trap

I'm not sure to see clearly the differences between those terms. Can someone provide a clear concise explanation?

Edit : the context question was : "What happens when you delete a pointer twice?" but knowing the differences between those terms is more important for me than just the answer.

like image 402
Laurent Avatar asked Aug 12 '13 10:08

Laurent


1 Answers

These aren't really particular to C++.

  • Abort is when you terminate the program, or a particular operation, because of a problem. There is a C++ library function std::abort, inherited from the C library, which kills the program as if by an external signal, and does not run destructors or clean-up.

  • An error is when something goes wrong. In C++, many kinds of errors are not necessarily detected immediately. C++ instead specifies undefined behavior, which may involve quiet memory corruption that may cause mysterious misbehavior later.

  • A failure is when a program does the wrong thing. This is pretty generic engineering term. The pointy-haired boss is probably more familiar with this concept than the others, because it's the only one a customer is really aware of.

  • A trap is when the program detects an error condition and takes some action accordingly.

So if you detect that the network went down, and show a message to the user such as "Could not continue; your document has been automatically saved" before quitting, then you have trapped an error and aborted, but nevertheless there was a failure.

like image 155
Potatoswatter Avatar answered Oct 03 '22 06:10

Potatoswatter