抛出,尝试并捕获(异常处理)C ++

时间:2019-04-15 17:32:25

标签: c++ buffer-manager

我在2个不同的文件中有2个函数,我尝试从一个重定向到另一个重定向进行捕获。更具体地说

void test6(){
//flushing file with pages still pinned. Should generate an error
for (i = 1; i <= num; i++) {
    bufMgr->readPage(file1ptr, i, page);
}

try
{
    bufMgr->flushFile(file1ptr);
    PRINT_ERROR("ERROR :: Pages pinned for file being flushed. Exception should have been thrown before execution reaches this point.");
}
catch(PagePinnedException e)
{

}

std::cout << "Test 6 passed" << "\n";

for (i = 1; i <= num; i++)
    bufMgr->unPinPage(file1ptr, i, true);

bufMgr->flushFile(file1ptr);

} 这是一个我希望我的flushfile函数通过的测试。我在我的flushFile函数中捕获了异常并且它可以正常工作,但似乎在test6中没有捕获它 我的函数flushFile是

void BufMgr::flushFile(const File* file) {
/* ============== */
/* YOUR CODE HERE */
/* ============== */
try{
    for (FrameId i = 0; i < numBufs; i++){
            if (bufDescTable[i].file == file){
                if (bufDescTable[i].pinCnt > 0){
                    throw (PagePinnedException("sfsddsf",bufDescTable[i].pageNo,bufDescTable[i].frameNo));
                }

                if (bufDescTable[i].dirty == true){
                        bufDescTable[i].file->writePage(bufPool[i]);
                        hashTable->remove(file,bufDescTable[i].pageNo);
                        bufDescTable[i].Clear();
                }
    }
}
}
catch(BadBufferException){

}

运行时我得到

  抛出PagePinnedException实例后调用

terminate

0 个答案:

没有答案