Theory on error handling?

后端 未结 14 836
情歌与酒
情歌与酒 2021-01-29 18:28

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 question

14条回答
  •  灰色年华
    2021-01-29 18:55

    Error handling is not accompanied by formal theory. It is too 'implementation specific' of a topic to be considered a science field (to be fair there is a great debate whether programming is a science on its own right).

    Nontheless it a good part of a developer's work (and thus his/hers life), so practical approaches and technical guidliness have been developed on the topic.

    A good view on the topic is presented by A. Alexandrescu, in his talk systematic error handling in C++

    I have a repository in GitHub where the techniques presented are implemented.

    Basically, what A.A does, is implement a class

    template
    class Expected { /* Implementation in the GitHub link */ };
    

    that is meant to be used as a return value. This class could hold either a return value of type T or an exception (pointer). The exception could be either thrown explictly or upon request, yet the rich error information is always available. An example usage would be like this

    int foo(); 
    // .... 
    Expected ret = foo();
    if (ret.valid()) {
        // do the work
    }
    else {
        // either use the info of the exception 
        // or throw the exception (eg in an exception "friendly" codebase)
    }
    

    While building this framework for error handling, A.A walks us through techniques and designs that produce successfull or poor error handling and what works or what not. He also gives his definitions of 'error' and 'error handling'

提交回复
热议问题