Home > Back-end >  How can I re-throw a CFileException?
How can I re-throw a CFileException?

Time:11-29

I know that I can re-throw a CFileException if I am inside a catch statement by using throw to bubble it up the chain.

But what about this:

CFile               fileRPT;
CFileException      ex;

if (!fileRPT.Open(L"SomeFile.txt", CFile::modeCreate | CFile::modeWrite, &ex))
{
    ex.ReportError();
    return;
}

It directly fills the CFileException object so I can't just use the keyword throw as we are not in a catch block.


If I try throw ex this is displayed:

enter image description here

CodePudding user response:

The clue was in the online documentation, about using AfxThrowFileException.

So:

AfxThrowFileException(ex.m_cause, ex.m_lOsError, ex.m_strFileName);

Based on the comments I have changed to using a smart pointer:

auto ex = std::make_unique<CFileException>();
if (!fileRPT.Open(strFileName, CFile::modeCreate | CFile::modeWrite, ex.get()))
{
    throw ex.release();
}
  • Related