Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Error Handling -- Good Sources of Example Code?

Just about every piece of example code everywhere omits error handling (because it "confuses the issue" that the example code is addressing). My programming knowledge comes primarily from books and web sites, and you seldom see any error handling in use at all there, let alone good stuff.

Where are some places to see good examples of C++ error handling code? Specific books, specific open-source projects (preferably with the files and functions to look at), and specific web pages or sites will all be gratefully accepted.

like image 983
Head Geek Avatar asked Oct 23 '08 19:10

Head Geek


People also ask

What is C error handling?

Most of the C or even Unix function calls return -1 or NULL in case of any error and set an error code errno. It is set as a global variable and indicates an error occurred during any function call. You can find various error codes defined in <error. h> header file.

What is good error handling?

A good error handler will log errors so they can be reviewed and analyzed. It will also provide the operator with a recall function to open the error log file and display errors. In addition, a good error handler logs all the errors, not just the ones that caused the error resolving to occur.

Does C have error handling?

The C programming language does not support exception handling nor error handling. It is an additional feature offered by C. In spite of the absence of this feature, there are certain ways to implement error handling in C. Generally, in case of an error, most of the functions either return a null value or -1.

What are the various types of errors in compilation process explain each type with example?

Types or Sources of Error – There are three types of error: logic, run-time and compile-time error: Logic errors occur when programs operate incorrectly but do not terminate abnormally (or crash).


1 Answers

Herb Sutter's and Andrei Alexandrescu's book C++ Coding Standards comes with a whole chapter on Error Handling and Exceptions including

  • Assert liberally to document internal assumptions and invariants
  • Establish a rational error handling policy, and follow it strictly
  • Distinguish between errors and non-errors
  • Design and write error-safe code
  • Prefer to use exceptions to report errors
  • Throw by value, catch by reference
  • Report, handle, and translate errors appropriately
  • Avoid exception specifications

Every topic also includes an example and I found it to be a very valuable resource.

like image 171
fhe Avatar answered Oct 09 '22 03:10

fhe