![](/img/trans.png)
[英]C++ What is the behavior of a try-catch statement if an exception is thrown, and that exception does not match the type of exception caught?
[英]c++ exception are not caught by catch(exception type)
这是我例外的一段代码:
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’
我正在OS X 10.8.5上使用i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
进行i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
#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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.