Diagnostics

Overview
In C/C+++, error reporting is done in different mechanisms. C Runtime functions return error numbers where as C++ throws exceptions. The following looks into this in detail.

Details
POSIX CRT functions such as log() sets error value in a global integer variable, errno. In addition in Windows environment, the error values set by Win32 APIs are retrieved from GetLastError().

The common convention is 0 means success and non zero indicate failures.  

In C++, exceptions could be thrown in a try block and caught in catch blocks. Any value such as int or a user defined object can be thrown from the try block which will be caught by corresponding catch block as shown in the example.

void printerror(int result)
{
    try
    {
        if (result == 1)    
            throw 10;
        else if (result == 2)
            throw "test";
        else if (result == 3)
            throw 2.0f;
        else if (result >= 4)
            throw 'a';
    }
    catch(const int& result)
    {
         cout << "int thrown: " << result << endl;
    }
    catch (const char*& result)
    {
        cout << "string thrown: " << result << endl;
    }
    catch (const float& result)
    {
        cout << "float thrown: " << result << endl;
    }
    catch (...)
    {
        cout << "something else thrown" << endl;
    }
}

    //prints:int thrown: 10
    printerror(1);
    //prints:string thrown: test
    printerror(2);
    //prints:float thrown: 2
    printerror(3);
    //prints:something else thrown
    printerror(4);

exception class
In C++ 11, a new family of classes were introduced to handle exceptions with exception class as its root.  The class exception is defined as below.
class exception 
{
public:
  exception () noexcept;
  exception (const exception&) noexcept;
  exception& operator= (const exception&) noexcept;
  virtual ~exception();
  virtual const char* what() const noexcept;
}

The derived classes overrides what() to return error string as seen in this example 2
struct ooops : exception 
{
  const char* what() const noexcept {return "unexpected error!";}
};

    /*prints
        unexpected error!
    */
    try 
    {
        //throws ooops
        throw ooops();
    } 
    catch (exception& ex) 
    {
        cerr << ex.what() << endl;
    }

    /*prints
        std::exception
    */
    try 
    {
        //throws exception
        throw static_cast<exception>(ooops());
    } 
    catch (exception& ex) 
    {
        cerr << ex.what() << endl;
    }
In addition, exceptions future_error and system_error return additional error information in error_code.

Standard Library Exception hierarchy
The following shows the hierarchy of exceptions. Some of these are thrown by the standard library. It can be grouped into three categories. User applications can also throw them.

This discusses various standard library exceptions with examples.

Advanced Exception Handling
The following discusses how to handle an exception, nested exceptions, uncaught and unexpected exceptions, termination. Also setting exception and termination handlers.

Handling Exception Failures➹
When an exception is unhandled, it leads to termination of the application. This discusses setting up custom handlers that's called before before termination.

System Errors➹
System error numbers are different in different platforms. e.g., file system errors. In C++11, a new exception class system_error along with helper classes - error_category, error_code, error_condition
were introduced to the diagnostic infrastructure to bring uniformity and develop cross platform applications.

Predefined System error enumerations
The standard library provides predefined enumeration classes to define error conditions for these categories: generic_category, iostream_category and future_categoty.

Summary of Examples
NameCategoryDescriptionGithubWandbox
ExampleC++ Exceptiongeneric exception handlersource    outputsource + output
Example 2exception classstd. library exception handlersource    outputsource + output
Example 3exception_ptrUsagesource    outputsource + output
Example 4exception_ptrmake_exceptionsource    outputsource + output
Example 5nested_exceptionexplicitsource    outputsource + output
Example 6nested_exceptionimplicitsource    outputsource + output
Example 7terminate()Usagesource    outputsource + output
Example 8uncaught_exception()Usagesource    outputsource + output
Example 9unexpected()Usagesource    outputsource + output
Example 10system_errorusing error_code and error_condition (POSIX)source    outputsource + output
Example 11system_errorusing error_code and error_condition (Windows)source    outputsource + output
Example 12system_errorfuture_categorysource    outputsource + output
Example 13system_erroriostream_categorysource    outputsource + output
Example 14system_errorgeneric_categorysource    outputsource + output
Example 15system_errorcustom error_categorysource    outputsource + output
Example 16errcUsagesource    outputsource + output
Example 17future_errcUsagesource    outputsource + output
Example 18io_errcUsagesource    outputsource + output


Included in Header file: <exception>   <future>  <iostream> <system_error>


No comments:

Post a Comment