• When I run a batch script using a GDAL/OGR program, I can detect if an error occured by checking ERRORLEVEL for the value 1.

  • When using GDAL/OGR in python or .NET, I get an exception when an error occurs.

How do I detect if a Warning was issued during the execution?

Even though a process can execute to the end despite of warnings, the output might be corrupt with respect to the data I expect. You could argue that I ought to check for these corruptions, but since I have control of the entire process, I would like to be able to stop when a warning is issued.

  • You can modify the error handling behaviour at the C level (see gdal.org/cpl__error_8h.html), including turning warning into errors and setting error handlers. I don't see anything that will expose that to non-C APIs though.
    – BradHards
    Dec 6, 2012 at 20:16
  • 1
    I think this question is more fitted to be asked at stackoverflow.com ?
    – nickves
    Dec 7, 2012 at 0:57
  • @BradHards: I would rather not dive into the C code and compile the stuff my self.
    – Chau
    Dec 7, 2012 at 7:38
  • 1
    @nickves: I have asked the question here because I expect the folks using GDAL/OGR to be using this forum.
    – Chau
    Dec 7, 2012 at 7:39

1 Answer 1


If using GDAL 1.10+ the python bindings allow you to specify a python callable as an error handler. However, these error handlers appear to be called in a separate thread and any exceptions raised do not propagate back to the main thread. So something like this won't work:

from osgeo import gdal

def error_handler(err_level, err_no, err_msg):

    if err_class >= gdal.CE_Warning:
        raise RuntimeError(err_level, err_no, err_msg) #this exception does not propagate back to main thread!
    else: print err_msg

if __name__=='__main__':

    #Test custom error handler
    gdal.Error(gdal.CE_Warning,2,'test warning message')

But you can do something like this:

from osgeo import gdal

class GdalErrorHandler(object):
    def __init__(self):

    def handler(self, err_level, err_no, err_msg):

if __name__=='__main__':

    handler=err.handler # Note don't pass class method directly or python segfaults
                        # due to a reference counting bug 
                        # http://trac.osgeo.org/gdal/ticket/5186#comment:4

    gdal.UseExceptions() #Exceptions will get raised on anything >= gdal.CE_Failure

        gdal.Error(gdal.CE_Warning,1,'gdal.CE_Warning warning')
    except Exception as e:
        print 'Operation raised an exception'
        print e
        print 'No exception'
        if err.err_level >= gdal.CE_Warning:
            raise RuntimeError(err.err_level, err.err_no, err.err_msg)
        print err.err_level, err.err_no, err.err_msg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.