2013-10-02 139 views
0

这里是一段代码,我有我的在异常:C++异常没有被捕获捕获(异常型)

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

    } 
    catch (HashNotFoundException &e) { 

    } 
    catch (HashNotFoundException e) { 

    } 
    catch (...) { 

    } 

异常内hashTable-生成>查找这样的:

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

这里是哈希表的查找方法签名

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

异常升级到顶级喜欢它的人的业务。

我使用MAC(狮子)和Xcode中(克++编译器)

任何想法,将不胜感激。

谢谢!

+0

你能分享 “查找” 功能的函数签名编制。具体检查一下它是否会限制它可以抛出的异常。 – NotAgain

+0

将其添加到问题中。在.cpp中没有限制。 –

+0

你能制作一个SSCE吗? http://sscce.org/另外它可能值得一提的是你正在使用的编译器和操作系统。 –

回答

1

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

例如代码以下版本的编译和对我的作品,outputing 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 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)在OS 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; } 
} 
+0

BadgerDBException :)我正在使用Apple LLVM编译器4.2。实质上,这正是我的代码所做的。唯一的区别是HashNotFoundException构造函数被声明为显式 - 但是从我读过的内容来看,这不应该有所作为。 –