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

Ivan Lucena ILucena at clarku.edu
Tue Nov 28 10:45:19 EST 2006

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:
        m_Dataset = (GDALDataset*) GDALOpen(pszFileName, GA_ReadOnly);

        if (m_Dataset == NULL)
            return E_FAIL;

        return S_OK;
        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.

Thanks in advance,


-----Original Message-----
From: gdal-dev-bounces at lists.maptools.org
[mailto:gdal-dev-bounces at lists.maptools.org] On Behalf Of Matt Hanson
Sent: Tuesday, November 28, 2006 10:17 AM
To: Gdal-dev at lists.maptools.org
Subject: RE: [Gdal-dev] GDAL CPLError and C++ Exceptions

Ok thanks, that answers my question.   I was thinking I could write my
own error handlers but sounds like that is not a good idea and I just
need to simply check error status after each GDAL call.  Martin - thanks
for the tips in the other posts.


From: Frank Warmerdam [mailto:warmerdam at pobox.com]
Sent: Mon 11/27/2006 8:35 PM
To: Matt Hanson
Cc: Gdal-dev at lists.maptools.org
Subject: Re: [Gdal-dev] GDAL CPLError and C++ Exceptions

Matt Hanson wrote:
> Hi, I recently got hung up on tracking down an GDAL error because I'm
> handling errors from CPLError.   I'd like to be able to handle those
> and create C++ Exceptions but I'm not clear on how to do so.   Anyone
> any code examples for how I might turn CPLErrors into Exceptions?   It
> like I should probably set the error handler, but what should that
> look like?   Would it simply examine the contents of the GDAL Error
> and message and decide to throw or not throw an exception? (which
would then
> be presumably caught in a user's application).


I just wanted to add a couple things:

  o You can also install your own error handlers to be called by
    You can find some more information about this at:


  o However, you should generally *not* throw exceptions from within
    an error handler, as GDAL does not guarantee proper cleanup in the
    of an exception deep in the internals.  At the very least you are
    to see memory leaks, and quite possible data structures could be
    in an improper state.  If you want to throw exceptions, it is best
to do
    it after the GDAL calls, as shown in the other email.

Best regards,
I set the clouds in motion - turn up   | Frank Warmerdam,
warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | President OSGeo,
http://osgeo.org <http://osgeo.org/> 

Gdal-dev mailing list
Gdal-dev at lists.maptools.org

More information about the Gdal-dev mailing list