[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:
{
    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.

Thanks in advance,

Ivan 


-----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
not
> handling errors from CPLError.   I'd like to be able to handle those
errors
> and create C++ Exceptions but I'm not clear on how to do so.   Anyone
have
> any code examples for how I might turn CPLErrors into Exceptions?   It
looks
> like I should probably set the error handler, but what should that
function
> look like?   Would it simply examine the contents of the GDAL Error
number
> and message and decide to throw or not throw an exception? (which
would then
> be presumably caught in a user's application).

Matt,

I just wanted to add a couple things:

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

      http://www.gdal.org/cpl__error_8h.html

  o However, you should generally *not* throw exceptions from within
such
    an error handler, as GDAL does not guarantee proper cleanup in the
face
    of an exception deep in the internals.  At the very least you are
likely
    to see memory leaks, and quite possible data structures could be
left
    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
http://lists.maptools.org/mailman/listinfo/gdal-dev




More information about the Gdal-dev mailing list