简体   繁体   English

C ++处理多个异常集合的方法?

[英]C++ approach to handling a collection of multiple exceptions?

In C++17, what is the proper pattern/approach to handling a collection of multiple exceptions? 在C ++ 17中,处理多个异常集合的正确模式/方法是什么?

Is there a C++ equivalent to C# AggregateException Class ? 是否有C ++等效于C# AggregateException类

(I'm aware that exception as flow control is an anti-pattern.) (我知道作为流控制的异常是一种反模式。)

This is not common problem I see in c++. 我在c ++中看到的这不是常见问题。 If you plan to use a library for multithread/multicore processing, you might wan't to check out what that library offers or how exceptions are handled. 如果您打算将库用于多线程/多核处理,则可能不希望查看该库提供的内容或如何处理异常。 If you just need that sort of capability yourself you can do something like the following (pseudocode): 如果您自己只需要这种功能,则可以执行以下操作(伪代码):

struct AggregateExcpetions {
  std::vector< std::variant< exception_type_1, exception_type_2, exception_type_3 > > m_exceptions;
}

Instead of using a variant it might be easier to just use a common base class - for example std::exception or perhaps std::runtime_error . 与其使用变体,不如仅使用公共基类可能会更容易-例如std::exceptionstd::runtime_error

struct AggregateExcpetions {
  std::vector< std::unique_ptr<std::exception> > m_exceptions;
}

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

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