Most advice concerning error handling boils down to a handful of tips and tricks (see this post for example). These hints are helpful but I think they don't answer all questions. I feel that I should design my application according to a certain philosophy, a school of thought that provides a strong foundation to build upon. Is there such a theory on the topic of error handling?

Here's a few practical questions:

  • How to decide if an error should be handled locally or propagated to higher level code?
  • How to decide between logging an error, or showing it as an error message to the user?
  • Is logging something that should only be done in application code? Or is it ok to do some logging from library code.
  • In case of exceptions, where should you generally catch them? In low-level or higher level code?
  • Should you strive for a unified error handling strategy through all layers of code, or try to develop a system that can adapt itself to a variety of error handling strategies (in order to be able to deal with errors from 3rd party libraries).
  • Does it make sense to create a list of error codes? Or is that old fashioned these days?

In many cases common sense is sufficient for developing a good-enough strategy to deal with error conditions. However, I would like to know if there is a more formal/"scholarly" approach?

PS: this is a general question, but C++ specific answers are welcome too (C++ is my main programming language for work).

Edit
Report