簡體   English   中英

c ++異常未被catch捕獲(異常類型)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM