[Gdal-dev] GDAL CPLError and C++ Exceptions

Mateusz Loskot mateusz at loskot.net
Tue Nov 28 12:17:16 EST 2006

Ivan Lucena wrote:
> Hi Matt, Martin, Frank, all,
>>> simply check error status after each GDAL call...
> That looks like that is the right thing to do. I just want to share the
> following piece of code with you guys where I am trying to use the same
> approach. I would like to know if I am going in the right direction, if
> you don't mind to take a quick look.
> // A C++ method:
> {
>     try
>     {
>         m_Dataset = (GDALDataset*) GDALOpen(pszFileName, GA_ReadOnly);
>         if (m_Dataset == NULL)
>         {
>             return E_FAIL;
>         }
>         return S_OK;
>     }
>     catch(...)
>     {
>         return E_FAIL;
>     }
> }
> I have debugged it a lot, forcing the error with invalid inputs (null
> pointer, invalid file name, invalid network address, etc.), and the
> result is that the caller of that method receives the exception nicely
> jumping to his own exception handler to do his own stuff as I would like
> it to do.


I'd like to share my comments:

1. GDAL calls do not throw C++ exceptions.
2. GDAL calls do not use C++ Standard Library features throwing
exceptions. OK, std::string class is used.
3. Even if GDALOpen will throw anything, your version is not exception
safe, unless
3.1 GDALOpen promises to release all resources on throw
3.2 You call GDALClose on catch, before return E_FAIL

Mateusz Loskot

More information about the Gdal-dev mailing list