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; }
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.
The following discusses how to handle an exception, nested exceptions, uncaught and unexpected exceptions, termination. Also setting exception and termination handlers.
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.
The standard library provides predefined enumeration classes to define error conditions for these categories: generic_category, iostream_category and future_categoty.
Summary of Examples
Name | Category | Description | Github | Wandbox |
---|---|---|---|---|
Example | C++ Exception | generic exception handler | source output | source + output |
Example 2 | exception class | std. library exception handler | source output | source + output |
Example 3 | exception_ptr | Usage | source output | source + output |
Example 4 | exception_ptr | make_exception | source output | source + output |
Example 5 | nested_exception | explicit | source output | source + output |
Example 6 | nested_exception | implicit | source output | source + output |
Example 7 | terminate() | Usage | source output | source + output |
Example 8 | uncaught_exception() | Usage | source output | source + output |
Example 9 | unexpected() | Usage | source output | source + output |
Example 10 | system_error | using error_code and error_condition (POSIX) | source output | source + output |
Example 11 | system_error | using error_code and error_condition (Windows) | source output | source + output |
Example 12 | system_error | future_category | source output | source + output |
Example 13 | system_error | iostream_category | source output | source + output |
Example 14 | system_error | generic_category | source output | source + output |
Example 15 | system_error | custom error_category | source output | source + output |
Example 16 | errc | Usage | source output | source + output |
Example 17 | future_errc | Usage | source output | source + output |
Example 18 | io_errc | Usage | source output | source + output |
Included in Header file: <exception> <future> <iostream> <system_error>
No comments:
Post a Comment