简体   繁体   English

定义自己的异常类的最佳实践?

[英]Best practices for defining your own exception classes?

I have some special exception cases that I want to throw and catch, so I want to define my own exception classes. 我有一些特殊的异常情况,我想抛出并捕获,所以我想定义自己的异常类。

What are the best practices for that? 有什么最好的做法? Should I inherit from std::exception or std::runtime_error ? 我应该继承std::exception还是std::runtime_error

Yes, it's good practice to inherit from std::runtime_error or the other standard exception classes like std::logic_error , std::invalid_argument and so on, depending on which kind of exception it is. 是的,最好从std::runtime_error其他标准异常类 继承 ,如std::logic_errorstd::invalid_argument等,具体取决于它是什么类型的异常。

If all the exceptions inherit some way from std::exception it's easy to catch all common errors by a catch(const std::exception &e) {...} . 如果所有异常都从std::exception exception继承某种方式,那么很容易通过catch(const std::exception &e) {...}捕获所有常见错误。 If you have several independent hierarchies this gets more complicated. 如果你有几个独立的层次结构,这会变得更加复杂。 Deriving from the specialized exception classes makes these exceptions carry more information, but how useful this really is depends on how you do your exception handling. 从专门的异常类派生会使这些异常带来更多信息,但这实际上有多么有用取决于您如何进行异常处理。

I'm not a C++ developer, but one thing we did in our C# code was create a base class exception for our framework, and then log the exception thrown in the constructor: 我不是C ++开发人员,但我们在C#代码中做的一件事是为我们的框架创建一个基类异常,然后记录构造函数中抛出的异常:

  public FrameworkException(string message, Exception innerException)
      : base(message, innerException)
  {
      log.Error(message, innerException);
  }

  ...

Any derived exception just has to invoke it's base constructor and we get consistent exception logging throughout. 任何派生异常只需调用它的基本构造函数,我们就可以获得一致的异常日志记录。 Not a big deal, but useful. 没什么大不了的,但很有用。

It is a good when exception is placed in some scope. 在某些范围内放置异常时,这是一件好事。 For instance class Manipulation can declare inside exception classes Error. 例如,类操作可以在异常类Error中声明。

and catch them like 并抓住他们

catch ( const Manipulation::InputError& error )
catch ( const Manipulation::CalculationError& error )

In such cases they can be just empty classes without any additional error information unless you design allows those exceptions fly much upper where you catch all standard exceptions. 在这种情况下,它们可以只是空类而没有任何其他错误信息,除非您设计允许这些异常在您捕获所有标准异常时飞得更高。

In my opinion it doesn't matter if you inherit from std::exception or not. 在我看来,如果你继承std :: exception并不重要。 For me the most important thing about defining exceptions are: 对我来说,定义异常最重要的是:

  1. Having the exception class names be useful and clear. 使异常类名称有用且清晰。
  2. Clearly documenting (writing comments) when exception will get thrown by a function or class method. 当函数或类方法抛出异常时,清楚地记录(写注释)。 This is the single biggest failure point in exception handling in my opinion. 在我看来,这是异常处理中最大的失败点。

It doesn't make a big difference, since std::runtime_error also inherits from std::exception . 它没有太大的区别,因为std::runtime_error也继承自std::exception You could argue that runtime error conveys more information about the exception, but in practice, people often just derive from the base exception class. 您可能会争辩说运行时错误会传达有关异常的更多信息,但实际上,人们通常只是从基类异常类派生而来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM