Skip Navigation Links
Skip Navigation LinksHome > ZipArchive > How to Use > Article
Exceptions Handling
Applies To: All

Introduction

  • The ZipArchive Library uses mostly 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 will work uniformly in STL and MFC version, use the CZipException::Throw() method.

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 Calls

Article ID: 0610222049
Back To Top Up