Exceptions Handling
Applies To: All

Introduction

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"));
// ... do some processing here
zip.Close();
}
catch(CZipException& ex)
{
// display the exception message
_tprintf(_T("Error while processing an archive: %s"),
(LPCTSTR)ex.GetErrorDescription());
// close the archive safely releasing resources;
// the archive will most probably be not usable after this call,
// if it was modified
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"));
// ... do some processing here
zip.Close();
}
catch(CException* ex)
{
// display the exception message
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;
// ... retrieve detailed information about the exception
}
else if (ex->IsKindOf( RUNTIME_CLASS( CFileException )))
{
CFileException* p = (CFileException*) ex;
// ... retrieve detailed information about the exception
}
else
{
//... and so on
}
ex->Delete();
// close the archive writing a central directory
// (makes sense if the archive was modified);
// the archive should be usable after this call
zip.Close(CZipArchive::afWriteDir);
}

See Also API Links

Article ID: 0610222049
Copyright © 2000 - 2022 Artpol Software - Tadeusz Dracz