Introduction
- The ZipArchive Library uses exceptions to notify about errors occurred while processing
an archive.
- If an exception is thrown while processing an archive, you should release the used
resources by calling CZipArchive::Close() with the
CZipArchive::afAfterException or
CZipArchive::afWriteDir value.
- The ZipArchive Library compiles by default with its own set of error messages. If
you want to disable their compilation (e.g. to make the compiled library smaller),
undefine _ZIP_ENABLE_ERROR_DESCRIPTION in the ZipException.cpp file.
- When you need to throw a CZipException in a way that
it works uniformly in STL and MFC version, use the CZipException::Throw()
method.
- The reason for the exception is stored in the CZipException::m_iCause
member variable and the error code reported by the system is stored in CZipException::m_iSystemError.
STL Version
In the STL version, the ZipArchive Library throws exceptions inherited from
std::exception
.
When catching exceptions, you should catch a reference to an exception object, not
a pointer to it.
Sample Code
CZipArchive zip;
try
{
zip.Open(_T("C:\\Temp\\test.zip"));
zip.Close();
}
catch(CZipException& ex)
{
_tprintf(_T("Error while processing an archive: %s"),
(LPCTSTR)ex.GetErrorDescription());
zip.Close(CZipArchive::afAfterException);
}
MFC Version
In the MFC version, the ZipArchive Library throws exceptions inherited from
CException
.
When catching exceptions, you should catch a pointer to an exception object and
delete the object after you have finished handling the exception.
Sample Code
CZipArchive zip;
try
{
zip.Open(_T("C:\\Temp\\test.zip"));
zip.Close();
}
catch(CException* ex)
{
TCHAR lpszError[1024];
ex->GetErrorMessage(lpszError, 1024);
_tprintf(_T("Error while processing an archive: %s"), lpszError);
if (ex->IsKindOf( RUNTIME_CLASS( CZipException )))
{
CZipException* p = (CZipException*) ex;
}
else if (ex->IsKindOf( RUNTIME_CLASS( CFileException )))
{
CFileException* p = (CFileException*) ex;
}
else
{
}
ex->Delete();
zip.Close(CZipArchive::afWriteDir);
}
See Also API Links