Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error code vs error condition

Tags:

c++

c++11

I don't quite get why do we need to make a distinction between error code (std::error_code) and an error condition(std::error_condition), aren't they the same thing? What are the advantages of an error condition vs error code?

like image 249
Lesswire Avatar asked May 22 '13 08:05

Lesswire


People also ask

What is error condition?

The ERROR condition is the implicit action for many conditions. This provides a common condition that can be used to check for a number of different conditions, rather than checking each condition separately.

What is one way in which exceptions are superior to error codes?

The biggest benefit exception handling has over error codes is that it changes the flow of execution, which is important for two reasons. When an exception occurs, the application is no longer following it's 'normal' execution path.


2 Answers

From http://en.cppreference.com/w/cpp/error/error_condition

std::error_condition is a platform-independent error code. Like std::error_code, it is uniquely identified by an integer value and a std::error_category, but unlike std::error_code, the value is not platform-dependent.

So, the advantage is your error code isn't specific to the platform you're working on when using std::error:condition.

With an std::error_code

Each std::error_code object holds a pair of error code originating from the operating system, or some low-level interface

So, the error_code will reference something specific to your platform, a piece of hardware etc etc.

It may be advantageous to use both. The error_condition is the "portable abstraction" so would be the generic error message to give to the user and the error_code would be the platform dependent information that would be useful for specific debug.

A typical implementation [of error_condition] holds one integer data member (the value) and a pointer to an std::error_category.

like image 156
Jimbo Avatar answered Sep 20 '22 23:09

Jimbo


The simplest answer to this question I found here: http://blog.think-async.com/2010/04/system-error-support-in-c0x-part-5.html.

  • class std::error_code - represents a specific error value returned by an operation (such as a system call).
  • class std::error_condition - something that you want to test for and, potentially, react to in your code.

I think it is applicable for C++11 too.

like image 45
Marat Abrarov Avatar answered Sep 18 '22 23:09

Marat Abrarov