c++ 异常未被 catch 捕获(异常类型)

标签 c++ exception

这是我有异常(exception)的一段代码:

try {
        hashTable->lookup(bufDescTable[clockHand].file, bufDescTable[clockHand].pageNo, dummyFrame);
    }
    catch (HashNotFoundException *e) {

    }
    catch (HashNotFoundException &e) {

    }
    catch (HashNotFoundException e) {

    }
    catch (...) {

    }

在 hashTable->lookup 中产生异常:

throw HashNotFoundException(file->filename(), pageNo);

这里是hashTable-lookup方法签名

 void BufHashTbl::lookup(const File* file, const PageId pageNo, FrameId &frameNo)  

异常会升级到最高级别,就像这与任何人无关。

我正在使用 Mac(Lion) 和 Xcode(编译器为 g++)

如有任何想法,我们将不胜感激。

谢谢!

最佳答案

我们确实需要一个完整的示例/更多信息来为您诊断此问题。

例如,以下版本的代码编译并为我工作,输出 HashNotFoundException &

请注意,代码与您的原始代码相比有一些细微的变化,但它们不应该是重要的。

但是它确实会生成以下警告:

example.cpp: In function ‘int main()’:
example.cpp:42: warning: exception of type ‘HashNotFoundException’ will be caught
example.cpp:38: warning:    by earlier handler for ‘HashNotFoundException’

我在操作系统上使用 i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1(基于 Apple Inc. build 5658)(LLVM build 2336.11.00) 进行编译X 10.8.5

#include <iostream>
#include <sstream>

struct BadgerDbException {};
struct PageId {};
struct FrameId {};

struct HashNotFoundException : public BadgerDbException {
  std::string name;
  PageId pageNo;
  HashNotFoundException(const std::string& nameIn, PageId pageNoIn)
    : BadgerDbException(), name(nameIn), pageNo(pageNoIn) {
    }
};

struct HashTable {
  void lookup(void* file, const PageId pageNo, FrameId &frameNo) {
    throw HashNotFoundException("a file", pageNo);
  }
};    

int main() {
  HashTable * hashTable = new HashTable;
  PageId a_Page_ID;
  FrameId dummyFrame;
  try {
    FrameId dummyFrame;
    hashTable->lookup(NULL, a_Page_ID, dummyFrame);
  }
  catch (HashNotFoundException *e) { std::cout<<"HashNotFoundException *"<<std::endl;}
  catch (HashNotFoundException &e) { std::cout<<"HashNotFoundException &"<<std::endl;}
  catch (HashNotFoundException e)  { std::cout<<"HashNotFoundException"<<std::endl; }
  catch (...)                      { std::cout<<"... exception"<<std::endl; }
}

关于c++ 异常未被 catch 捕获(异常类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19128606/

相关文章:

c++ - 快速排序导致计算器溢出?

c++ - 如何更改子类方法中的参数?

C++ 创建类似于 while、if 或 for 的函数

c++ - 读取功能失败时的成功消息

java - 正确使用 RuntimeException?

java - GWT开发模式错误

c++ - 检测计算机是否是 NetApp 文件管理器? (非托管 C++)

c++ - 3D 数组作为纹理在 CUDA 中写入和读取

c++ - 抛出一个 catch block (C++)是否有效?

python - 如果某个时间正在执行方法,则重复