Skip to main content

There are some python 'only' rules stated at the 'Python GotchasPython Gotchas' page.

At the beginning they state that:

By default, the GDAL and OGR Python bindings do not raise exceptions when errors occur. Instead they return an error value such as None and write an error message to sys.stdout.

So first enable the use of exceptions, by issuing gdal.UseExceptions() somewhere in the beginning of your script. And secondly catch if any exceptions and do whatever you want with them (including nothing)

gdal.UseExceptions() # Enable errors ... try: band.GetStatisticsts() except RuntimeError: # <- Check first what exception is being thrown pass 

If you're curious here's a link from python's wiki where they describe the methods of exception handling.

As far as I can tell, using gdal.UseExceptions() will normalize the behaviour of all the methods in the gdal lib, by making those methods to use the Python Exceptions. If you really want to ignore all exceptions put the problematic part of your code inside a try/except block eg.:

>>> try: print "1" print "2" a = 0 b = 1 c = b/a # <- I just divided by zero. except: print "Everything is ok" # <- but Everything is Ok. # If you really want to be silent replace with pass >>> 1 2 'Nothing to see here' 

Just be careful, because that way when an exception happens, your script will silently break from the procedure without any indication of what went wrong.

Also I want to point a couple of things as well:

  1. If you're opt to go that way, you're responsible if your program does. You're choosing to turn a blind eye at any errors that come in your way.
  2. If you catch ALL the exceptions, you will also catch the 'KeyboardInterrupt' exception as well. No more ctrl-c (if you're inside a try/except block).
  3. Your code at some point is encountering a processing error. Since you're not using gdal.UseExceptions() you're experiencing that error as a sys.stdout response. Use a try/except block at that point to plug that hole.

There are some python 'only' rules stated at the 'Python Gotchas' page.

At the beginning they state that:

By default, the GDAL and OGR Python bindings do not raise exceptions when errors occur. Instead they return an error value such as None and write an error message to sys.stdout.

So first enable the use of exceptions, by issuing gdal.UseExceptions() somewhere in the beginning of your script. And secondly catch if any exceptions and do whatever you want with them (including nothing)

gdal.UseExceptions() # Enable errors ... try: band.GetStatisticsts() except RuntimeError: # <- Check first what exception is being thrown pass 

If you're curious here's a link from python's wiki where they describe the methods of exception handling.

As far as I can tell, using gdal.UseExceptions() will normalize the behaviour of all the methods in the gdal lib, by making those methods to use the Python Exceptions. If you really want to ignore all exceptions put the problematic part of your code inside a try/except block eg.:

>>> try: print "1" print "2" a = 0 b = 1 c = b/a # <- I just divided by zero. except: print "Everything is ok" # <- but Everything is Ok. # If you really want to be silent replace with pass >>> 1 2 'Nothing to see here' 

Just be careful, because that way when an exception happens, your script will silently break from the procedure without any indication of what went wrong.

Also I want to point a couple of things as well:

  1. If you're opt to go that way, you're responsible if your program does. You're choosing to turn a blind eye at any errors that come in your way.
  2. If you catch ALL the exceptions, you will also catch the 'KeyboardInterrupt' exception as well. No more ctrl-c (if you're inside a try/except block).
  3. Your code at some point is encountering a processing error. Since you're not using gdal.UseExceptions() you're experiencing that error as a sys.stdout response. Use a try/except block at that point to plug that hole.

There are some python 'only' rules stated at the 'Python Gotchas' page.

At the beginning they state that:

By default, the GDAL and OGR Python bindings do not raise exceptions when errors occur. Instead they return an error value such as None and write an error message to sys.stdout.

So first enable the use of exceptions, by issuing gdal.UseExceptions() somewhere in the beginning of your script. And secondly catch if any exceptions and do whatever you want with them (including nothing)

gdal.UseExceptions() # Enable errors ... try: band.GetStatisticsts() except RuntimeError: # <- Check first what exception is being thrown pass 

If you're curious here's a link from python's wiki where they describe the methods of exception handling.

As far as I can tell, using gdal.UseExceptions() will normalize the behaviour of all the methods in the gdal lib, by making those methods to use the Python Exceptions. If you really want to ignore all exceptions put the problematic part of your code inside a try/except block eg.:

>>> try: print "1" print "2" a = 0 b = 1 c = b/a # <- I just divided by zero. except: print "Everything is ok" # <- but Everything is Ok. # If you really want to be silent replace with pass >>> 1 2 'Nothing to see here' 

Just be careful, because that way when an exception happens, your script will silently break from the procedure without any indication of what went wrong.

Also I want to point a couple of things as well:

  1. If you're opt to go that way, you're responsible if your program does. You're choosing to turn a blind eye at any errors that come in your way.
  2. If you catch ALL the exceptions, you will also catch the 'KeyboardInterrupt' exception as well. No more ctrl-c (if you're inside a try/except block).
  3. Your code at some point is encountering a processing error. Since you're not using gdal.UseExceptions() you're experiencing that error as a sys.stdout response. Use a try/except block at that point to plug that hole.
added 1459 characters in body
Source Link
nickves
  • 11.7k
  • 3
  • 44
  • 79

There are some python 'only' rules stated at the 'Python Gotchas' page.

At the beginning they state that:

By default, the GDAL and OGR Python bindings do not raise exceptions when errors occur. Instead they return an error value such as None and write an error message to sys.stdout.

So first enable the use of exceptions, by issuing gdal.UseExceptions() somewhere in the beginning of your script. And secondly catch if any exceptions and do whatever you want with them (including nothing)

gdal.UseExceptions() # Enable errors ... try: band.GetStatisticsts() except RuntimeError: # <- Check first what exception is being thrown pass 

If you're curious here's a link from python's wiki where they describe the methods of exception handling.

As far as I can tell, using gdal.UseExceptions() will normalize the behaviour of all the methods in the gdal lib, by making those methods to use the Python Exceptions. If you really want to ignore all exceptions put the problematic part of your code inside a try/except block eg.:

>>> try: print "1" print "2" a = 0 b = 1 c = b/a # <- I just divided by zero. except: print "Everything is ok" # <- but Everything is Ok. # If you really want to be silent replace with pass >>> 1 2 'Nothing to see here' 

Just be careful, because that way when an exception happens, your script will silently break from the procedure without any indication of what went wrong.

Also I want to point a couple of things as well:

  1. If you're opt to go that way, you're responsible if your program does. You're choosing to turn a blind eye at any errors that come in your way.
  2. If you catch ALL the exceptions, you will also catch the 'KeyboardInterrupt' exception as well. No more ctrl-c (if you're inside a try/except block).
  3. Your code at some point is encountering a processing error. Since you're not using gdal.UseExceptions() you're experiencing that error as a sys.stdout response. Use a try/except block at that point to plug that hole.

There are some python 'only' rules stated at the 'Python Gotchas' page.

At the beginning they state that:

By default, the GDAL and OGR Python bindings do not raise exceptions when errors occur. Instead they return an error value such as None and write an error message to sys.stdout.

So first enable the use of exceptions, by issuing gdal.UseExceptions() somewhere in the beginning of your script. And secondly catch if any exceptions and do whatever you want with them (including nothing)

gdal.UseExceptions() # Enable errors ... try: band.GetStatisticsts() except RuntimeError: # <- Check first what exception is being thrown pass 

If you're curious here's a link from python's wiki where they describe the methods of exception handling.

There are some python 'only' rules stated at the 'Python Gotchas' page.

At the beginning they state that:

By default, the GDAL and OGR Python bindings do not raise exceptions when errors occur. Instead they return an error value such as None and write an error message to sys.stdout.

So first enable the use of exceptions, by issuing gdal.UseExceptions() somewhere in the beginning of your script. And secondly catch if any exceptions and do whatever you want with them (including nothing)

gdal.UseExceptions() # Enable errors ... try: band.GetStatisticsts() except RuntimeError: # <- Check first what exception is being thrown pass 

If you're curious here's a link from python's wiki where they describe the methods of exception handling.

As far as I can tell, using gdal.UseExceptions() will normalize the behaviour of all the methods in the gdal lib, by making those methods to use the Python Exceptions. If you really want to ignore all exceptions put the problematic part of your code inside a try/except block eg.:

>>> try: print "1" print "2" a = 0 b = 1 c = b/a # <- I just divided by zero. except: print "Everything is ok" # <- but Everything is Ok. # If you really want to be silent replace with pass >>> 1 2 'Nothing to see here' 

Just be careful, because that way when an exception happens, your script will silently break from the procedure without any indication of what went wrong.

Also I want to point a couple of things as well:

  1. If you're opt to go that way, you're responsible if your program does. You're choosing to turn a blind eye at any errors that come in your way.
  2. If you catch ALL the exceptions, you will also catch the 'KeyboardInterrupt' exception as well. No more ctrl-c (if you're inside a try/except block).
  3. Your code at some point is encountering a processing error. Since you're not using gdal.UseExceptions() you're experiencing that error as a sys.stdout response. Use a try/except block at that point to plug that hole.
deleted 100 characters in body
Source Link
nickves
  • 11.7k
  • 3
  • 44
  • 79

There are some python 'only' rules stated at the 'Python Gotchas' page. Basically there a couple python specific rules that are custom made for whatever reasons.

Anyway atAt the beginning they state that:

By default, the GDAL and OGR Python bindings do not raise exceptions when errors occur. Instead they return an error value such as None and write an error message to sys.stdout.

So first enable the use of exceptions, by issuing gdal.UseExceptions() somewhere in the beginning of your script. And secondly catch if any exceptions and do whatever you want with them (including nothing)

gdal.UseExceptions() # Enable errors ... try: band.GetStatisticsts() except RuntimeError: # <- Check first what exception is being thrown pass 

If you're curious here's thea link from python's wiki where they describe the methods of exception handling.

There are some python 'only' rules stated at the 'Python Gotchas' page. Basically there a couple python specific rules that are custom made for whatever reasons.

Anyway at the beginning they state that:

By default, the GDAL and OGR Python bindings do not raise exceptions when errors occur. Instead they return an error value such as None and write an error message to sys.stdout.

So first enable the use of exceptions, by issuing gdal.UseExceptions() somewhere in the beginning of your script. And secondly catch if any exceptions and do whatever you want with them (including nothing)

gdal.UseExceptions() # Enable errors ... try: band.GetStatisticsts() except RuntimeError: # <- Check first what exception is being thrown pass 

If you're curious here's the link from python's wiki where they describe the methods of exception handling.

There are some python 'only' rules stated at the 'Python Gotchas' page.

At the beginning they state that:

By default, the GDAL and OGR Python bindings do not raise exceptions when errors occur. Instead they return an error value such as None and write an error message to sys.stdout.

So first enable the use of exceptions, by issuing gdal.UseExceptions() somewhere in the beginning of your script. And secondly catch if any exceptions and do whatever you want with them (including nothing)

gdal.UseExceptions() # Enable errors ... try: band.GetStatisticsts() except RuntimeError: # <- Check first what exception is being thrown pass 

If you're curious here's a link from python's wiki where they describe the methods of exception handling.

Source Link
nickves
  • 11.7k
  • 3
  • 44
  • 79
Loading