Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
  • 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.

share|improve this question
You can modify the error handling behaviour at the C level (see, 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 '12 at 20:16
I think this question is more fitted to be asked at ? – nickves Dec 7 '12 at 0:57
@BradHards: I would rather not dive into the C code and compile the stuff my self. – Chau Dec 7 '12 at 7:38
@nickves: I have asked the question here because I expect the folks using GDAL/OGR to be using this forum. – Chau Dec 7 '12 at 7:39

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 

    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
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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