C ++ - 查找捕获的缺省异常的类型 [英] C++ - finding the type of a caught default exception

查看:169
本文介绍了C ++ - 查找捕获的缺省异常的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有:

try
{
 externalLibrary::doSomething();
}
catch (std::exception &e)
{
 //yay I know what to do
}
catch (...)
{
 //darn, I've no idea what happened!
}

可能会出现异常,来自或为什么 - 在一些外部库没有调试信息。有没有办法找到被抛出,或以其他方式获取任何数据相关联?他们可能会:

There might be cases you get an exception and you don't know where it's coming from or why - in some external library with no debug info. Is there a way to find what was thrown, or otherwise obtain any data associated with it? They might be doing:

throw myStupidCustomString("here is some really useful information");

但我永远不知道是否捕获 ...

But I'd never know if I catch ...

推荐答案

因为C ++是静态类型的,你必须捕获一个已知的类型。但是,您可以调用处理在调用它们的点未知的异常类型的外部函数(或一组函数)。如果这些处理程序都有已知类型,您可以注册它们以进行动态尝试。

Because C++ is statically typed, you must catch a known type. However, you can call an external function (or set of functions) which handle exception types unknown at the point you call them. If these handlers all have known types, you can register them to be dynamically tried.

struct myStupidCustomString {
  myStupidCustomString(char const *what) : what (what) {}
  char const *what;
};

void throws() {
  throw myStupidCustomString("here is some really useful information");
}

// The external library can provide a function, or you can provide a wrapper, which
// extracts information from "unknown" exception types.
std::string extract_from_unknown_external_exception() {
  try { throw; }
  catch (myStupidCustomString &e) {
    return e.what;
  }
  catch (...) {
    throw;  // Rethrow original exception.
  }
}

使用

void example() {
  try { throws(); }
  catch (...) {
    try {
      std::string extracted = extract_from_unknown_external_exception();
      std::cout << "extracted: " << extracted << '\n';
    }
    catch (...) {
      // Chain handlers for other types; e.g. exception types from other libraries.
      // Or do something generic for the unknown exception.

      // Or rethrow the original unknown exception:
      throw;
    }
  }
}

处理程序链

typedef std::string Extract();
std::vector<Extract*> chain (1, &extract_from_unknown_external_exception);
// Chain would normally be initialized using whatever scheme you prefer for
// initializing global objects.
// A list or other container (including a manual linked list that doesn't
// require dynamic allocation) may be more appropriate, depending on how you
// want to register and unregister handlers.
std::string process_chain() {
  for (std::vector<Extract*>::iterator x = chain.begin(); x != chain.end(); ++x) {
    try {
      return (*x)();
    }
    catch (...) {}  // That handler couldn't handle it.  Proceed to next.
  }
  throw;  // None could handle it, rethrow original exception.
}

void example() {
  try { throws(); }
  catch (...) {
    try {
      std::string extracted = process_chain();
      std::cout << "extracted: " << extracted << '\n';
    }
    catch (...) {
      throw;  // Rethrow unknown exception, or otherwise handle it.
    }
  }
}

最后,您可以使用这些来提取实施公开的任何其他信息。 C ++ 0x也以便携式方式暴露了一些细节;看看std :: exception_ptr。

Finally, if you know implementation specifics, you can use those to extract whatever additional information your implementation exposes. C++0x exposes some specifics in a portable way, too; look at std::exception_ptr.

这篇关于C ++ - 查找捕获的缺省异常的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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