错误处理理论? [英] Theory on error handling?

查看:96
本文介绍了错误处理理论?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于错误处理的大多数建议都归结为一些提示和技巧(参见这篇文章)。这些提示是有帮助的,但我认为他们不回答所有问题。我觉得我应该按照一定的理念来设计我的应用,这是一个提供坚实基础的思想学派。有没有关于错误处理主题的理论?



以下是一些实际问题:




  • 如何决定是否应该在本地处理错误或传播到更高级别的代码?

  • 如何在记录错误或将其显示为错误消息之间进行判断用户?

  • 是否记录应该在应用程序代码中完成的内容?或者可以从图书馆代码进行一些日志记录。

  • 如果出现异常,您应该在哪里抓住他们?在低级别或更高级别的代码?

  • 如果您通过所有层次的代码来争取统一的错误处理策略,或者尝试开发一种可以适应各种错误的系统处理策略(以便能够处理来自第三方库的错误)。

  • 创建错误代码列表是否有意义?还是现在这些老式?



在许多情况下,常识足以开发出一个足够好的策略来应对错误条件。但是,我想知道是否有更正式的/学术的方法?



PS:这是一个一般的问题,但C ++的具体答案也是受欢迎的C ++是我的主要编程语言。)

解决方案

几年前,我想到了同样的问题:) / p>

在搜索和阅读了几件事之后,我认为我发现的最有趣的参考是来自Andy Longshaw和Eoin Woods的错误的生成,处理和管理模式。这是一个简短而有系统的尝试来掩盖你提到的基本习语和其他一些。



这些问题的答案是有争议的,但上面的作者是勇敢的在会议中露面,然后把想法放在纸上。


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).

解决方案

A couple of years ago I thought exactly about the same question :)

After searching and reading several things, I think that the most interesting reference I found was Patterns for Generation, Handling and Management of Errors from Andy Longshaw and Eoin Woods. It is a short and systematic attempt to cover the basic idioms you mention and some others.

The answer to these questions is quite controversial, but the authors above were brave enough to expose themselves in a conference, and then put their thoughts on paper.

这篇关于错误处理理论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆